首页 > 解决方案 > 隐藏行网络抓取 html 表的问题

问题描述

我对网络抓取相对较新,我真的不知道为什么这不起作用,我认为这与我想要抓取的表中的隐藏行有关。

我正在尝试通过 for 循环将一个相当简单的表提取到熊猫数据框中。但是,当我尝试使用 find_all('tr') 来提取行时,我最终得到了一个完全空的集合。

这是我的代码:

url = 'https://pjammcycling.com/home;mode=search;sort=worldRank;view=table;asc=true;f1=France;dsply=100'

html = requests.get(url)
soup = BeautifulSoup(html.text, 'lxml')
table_body = soup.find('tbody')
rows = table_body.find_all('tr')

当我打印(行)时,我得到:

[]

我要使用的 for 循环是:

labels = []
data = []

for row in rows:
    labels.append(str(row.find_all('td')[0].text))
    data.append(str(row.find_all('td')[1].text))

cols = {'Field': labels, 'Data': data}
df = pd.DataFrame(cols)

任何帮助都将不胜感激,因为我整个早上都在用头撞墙试图让它工作:(我的谷歌搜索都没有为我清除这个问题

先感谢您!

标签: pythonhtmlweb-scraping

解决方案


Your tbody is empty when retrieved. In the browser it is filled by javascript. Here is the extract of the raw html:

<tbody _ngcontent-sc18=""><!----></tbody>

It is useless as is for BeautifulSoup. Maybe you could try that: https://pythonprogramming.net/javascript-dynamic-scraping-parsing-beautiful-soup-tutorial/


推荐阅读