如何在 BeautifulSoup 中组合解析参数

How to combine parsing arguments in BeatifulSoup

这是我第一次尝试直接从书中抄袭。 我设法实现了我所需要的,但是我想我可以跳过 1 个步骤,如果我知道怎么做的话。

import requests
from bs4 import BeautifulSoup as bs

page = requests.get("https://finance.yahoo.com/")
print(page.status_code)

soup = bs(page.content, 'html.parser')


res = soup.find('ul', class_='Carousel-Slider Pos(r) Whs(nw)')

ticker = res.find_all('li')

我想我可以合并最后两行,如果我知道怎么做....

如有任何帮助,我们将不胜感激。 雷.

如果combine是chain的意思,可以这样写:

ticker = soup.find('ul', class_='Carousel-Slider Pos(r) Whs(nw)').find_all('li')

css selectors - 使用标签或属性 id 而不是 class 也是更好的策略,因为前两个比最后一个更静态:

ticker = soup.select('ul.Carousel-Slider li[id*="marketsummary-itm"]')