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

时间:2019-10-25 16:07:59

标签: python beautifulsoup request screen-scraping scrape

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

`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())`

我的命令提示符没有响应。它什么也不打印。我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

soup.find_all(class_="balancedHeadline")返回的列表为空。似乎没有“ balancedHeadline”类。我没有检查类方案,但您至少会得到class_="css-1m5bs2v esl82me0"的一些结果。

答案 1 :(得分:1)

尝试此代码。

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())