我正在尝试抓取,但我的命令提示符未打印任何内容。我究竟做错了什么?

I am trying to scrape but my command prompt prints nothing. What am I doing wrong?

我正在尝试打印标题。这是我的代码:

`import requests
from bs4 import BeautifulSoup

base_url = 'http://www.nytimes.com'
r = requests.get(base_url)
r_html = r.text
soup = BeautifulSoup(r.text, features="lxml")

for story_heading in soup.find_all(class_="balancedHeadline"):
    if story_heading.a:
        print(story_heading.a.text.replace("\n", " ").strip())
    else:
        print(story_heading.contents[0].strip())`

我的命令提示符没有任何反应。它什么都不打印。我的代码有什么问题?

列表 soup.find_all(class_="balancedHeadline") returns 为空。好像没有class"balancedHeadline"。我没有检查 class 方案,但你至少会得到 class_="css-1m5bs2v esl82me0".

的一些结果

试试这个代码。

import requests
from bs4 import BeautifulSoup

base_url = 'http://www.nytimes.com'
r = requests.get(base_url)
r_html = r.text
soup = BeautifulSoup(r.text)

for story_heading in soup.findAll(class_="css-1m5bs2v esl82me0"):
    print(story_heading.get_text())