繁体   English   中英

Python beautifulsoup,抓取网站中的表格

[英]Python beautifulsoup, scraping a table in a website

我最近开始通过python库beautifulsoup4开始对Web抓取感兴趣,我的目标是获取有关covid-19案例的数据(在摩洛哥是一个好的开始); 我的信息所在的网站是:“https://www.worldometers.info/coronavirus/”有一个包含所有信息的大表,我尝试做这样的事情:

U = 'https://www.worldometers.info/coronavirus/'
response = requests.get(U)
html_soup = BeautifulSoup(response.text, 'html.parser')
info = html_soup.find_all('tr', class_='even')
print(info)

但是信息列表是空的,我尝试更改类和标签,但似乎我做错了什么(morrocco 信息在第 30 行)

更新:我使用 selenium 来获取我的信息,顺便说一句,我使用 google collab,所以这有点困难,但现在更好的方式是 python 笔记本格式的解决方案的 Da 链接

数据是通过 JS 动态生成的。 如果您进入浏览器并在开发工具中禁用 Javascript,您将看到没有带有<tr class="even">元素

您要么需要使用HTTP Trace 之类的工具(通过某些 Web API)找出获取数据的位置,要么使用Selenium 之类的工具来运行 Javascript 来加载 HTML。

您想传递标签属性的字典:

info = html_soup.find_all('tr', {'class':'even'})

这给了我一个完整的国家列表。

url       = 'https://www.worldometers.info/coronavirus/'

response  = requests.get(url)

html_soup = BeautifulSoup(response.text, 'html.parser')
info      = html_soup.find_all('a', {'class':'mt_a'})


print(info[29].text) # returns Marocco


# All the rest

for i in info:  
  print(i.text)

暂无
暂无

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

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