python - 隐藏行网络抓取 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)
任何帮助都将不胜感激,因为我整个早上都在用头撞墙试图让它工作:(我的谷歌搜索都没有为我清除这个问题
先感谢您!
解决方案
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/
推荐阅读
- python - 合并具有不同长度的屏蔽数组的二维列表
- vba - 过滤列表的算法
- omniauth - 如何在 OmniAuth 中向不同的 url 发送回调?
- ecmascript-6 - Symbol.keyFor() 是如何工作的?
- python-3.x - 当 asyncio.StreamReader.read() 被调用时,会发生哪种类型的错误?
- amazon-web-services - AWS:如何在 AWS 上持续部署静态网站
- sublimetext3 - Sublime Text:以某种格式更改选定的文本
- scala - Scala sbt 库依赖项 - 找不到模块错误
- react-native - 如何使用@shoutem/ui DropDownMenu 设置多项选择?
- scala - ScalaTest:使用检查器和断言的地方