Python BeautifulSoup找不到<tr>标记

时间:2020-08-28 21:05:02

标签: python html beautifulsoup

我正在尝试使用BeautifulSoup模块获取数据,但是我无法从网站获取所需标签,返回None,也不想使用 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)

1 个答案:

答案 0 :(得分:0)

total_row_world odd中没有ct这样的类,但有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)

输出:

<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>
相关问题