繁体   English   中英

美丽的汤不返回任何东西

[英]Beautiful Soup not returning anything

嗨,我正在尝试使用 Beautiful Soup 从网站上抓取网页并打印事实。 这是网站https://fungenerators.com/random/facts/animal/weasel 我试图通过网络抓取事实,尽管它总是最终打印 [] 知道我的代码有什么问题吗?

from urllib.request import urlopen
from bs4 import BeautifulSoup

scrape = "https://fungenerators.com/random/facts/animal/weasel"

request_page = urlopen(scrape)
page_html = request_page.read()
request_page.close()

html_soup = BeautifulSoup(page_html, 'html.parser')

fact = html_soup.find_all('div', class_="wow fadeInUp animated animated")

print(fact)

您的代码有两个问题:

  1. 您想要的元素位于h2标签下,而不是div

  2. 由于某些数据是动态加载的,因此类名发生了变化,并删除了“动画”一词的第二次出现。 而不是类名是wow fadeInUp animated animated它是wow fadeInUp animated

请参见以下示例:

from urllib.request import urlopen
from bs4 import BeautifulSoup

scrape = "https://fungenerators.com/random/facts/animal/weasel"

request_page = urlopen(scrape)
page_html = request_page.read()
request_page.close()

html_soup = BeautifulSoup(page_html, 'html.parser')

fact = html_soup.find_all('h2', class_="wow fadeInUp animated")

print(fact)

(由于只有一个标签,您可能需要考虑使用find()而不是find_all() ,以便使用.text方法获取文本):

...
fact = html_soup.find('h2', class_="wow fadeInUp animated").text

改用我的代码!!!

import requests
from bs4 import BeautifulSoup

response = requests.get('https://fungenerators.com/random/facts/animal/weasel')

soup = BeautifulSoup(response.content, 'html.parser')

result = soup.select('div.wow.fadeInUp.animated.animated')

print(result[0].text)

结果将是:

Random Weasel  Fact

或者,如果您不想使用 css 选择器,那么您可以执行以下操作:

import requests
from bs4 import BeautifulSoup

response = requests.get('https://fungenerators.com/random/facts/animal/weasel')

soup = BeautifulSoup(response.content, 'html.parser')

result = soup.find_all('h2', class_="wow fadeInUp animated")

print(result[0].text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM