簡體   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