[英]Python BeautifulSoup can't find <tr> tag
我正在嘗試使用 BeautifulSoup 模塊獲取數據,但我無法從網站獲取我想要的標簽,返回無,我也不想使用Selenium
import requests
from bs4 import BeautifulSoup as bs
site = "https://www.worldometers.info/coronavirus/#countries"
r = requests.get(site)
soup = bs(r.text,"html.parser")
ct = soup.find("table",id="main_table_countries_today")
ct2 = ct.find("tr",class_="total_row_world odd")
print(ct2)
ct
中沒有 class 作為total_row_world odd
,而是 class total_row_world
:
import requests
from bs4 import BeautifulSoup as bs
site = "https://www.worldometers.info/coronavirus/#countries"
r = requests.get(site)
soup = bs(r.text,"html.parser")
ct = soup.find("table",id="main_table_countries_today")
ct2 = ct.find("tr",class_="total_row_world")
print(ct2)
編輯:
經過更深入的調查,您似乎需要像selenium
這樣的工具來獲取您正在尋找的動態數據:
from bs4 import BeautifulSoup
import urllib.request
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
site = "https://www.worldometers.info/coronavirus/#countries"
driver.get(site)
content = driver.page_source
soup = BeautifulSoup(content, 'html.parser')
ct = soup.find("table",id="main_table_countries_today")
ct2 = ct.find("tr",class_="total_row_world odd")
print(ct2)
OUTPUT:
<tr class="total_row_world odd" role="row">
<td></td>
<td style="text-align:left;">World</td>
<td class="sorting_1">24,858,356</td>
<td>+242,396</td>
<td>839,702</td>
<td>+4,736</td>
<td>17,224,364</td>
<td>6,794,290</td>
<td>61,529</td>
<td>3,189</td>
<td>107.7</td>
<td></td>
<td></td>
<td></td>
<td data-continent="all" style="display:none">All</td>
</tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.