解析beautifulsoup4中的类

2024-10-05 13:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上我想访问html表中的元素。你知道吗

这是我的密码:

r = requests.get('http://www.google.com/finance?q=NYSE%3Aibm&ei=Hz4oVZq-PISjiQKYu4GoAQ')

soup = BeautifulSoup(r.content)

td = soup.find_all('td', class_='ctsymbol')

我什么都得不到…[]

我在同一个td上尝试了这种方法,但这次是在本地文本文件上,似乎效果不错。 我做错什么了?你知道吗


Tags: comhttp元素密码gethtmlwwwgoogle
1条回答
网友
1楼 · 发布于 2024-10-05 13:19:43

页面中根本没有这样的元素:

>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get('http://www.google.com/finance?q=NYSE%3Aibm&ei=Hz4oVZq-PISjiQKYu4GoAQ')
>>> soup = BeautifulSoup(r.content)
>>> {c for e in soup.find_all('td') if 'class' in e.attrs for c in e['class']}
set(['name', 'val', 'p', 'i', 'period', 'itxt', 'lft', 't', 'key', 'colHeader', 'linkbtn'])

这是在所服务的HTML中<td>元素上使用的所有类的集合。考虑到您不能依赖浏览器开发工具中的元素树,因为它们反映了JavaScript代码运行后的页面。你知道吗

相关问题 更多 >

    热门问题