【发布时间】:2017-08-02 13:51:45
【问题描述】:
我正在尝试为在线绘图的轴以及与之相关的一些功能(例如文本的颜色)抓取文本,但很少使用抓取,因此非常感谢您的帮助。对于经常使用刮板的人来说,这可能是一个简单的解决方法。这是我的代码:
from bs4 import BeautifulSoup
import requests
def get_IPF_transcriptome_groups():
url = "https://research.cchmc.org/pbge/lunggens/lungDisease/celltype_IPF.html?cid=1"
r = requests.get(url)
data=r.text
soup = BeautifulSoup(data)
for d in soup.find('div', attrs={'id':'wrapper'}).find(
'div', attrs={'class':'content'}).find(
'div', attrs={'id':'ResPanel'}).find(
'table', attrs={'id':'maintable'}).find(
'tbody'):
print(d)
我收到一个错误:
'tbody'):
TypeError: 'NoneType' object is not iterable
我认为代码无法通过表体。我要解析的实际文本通过其他几个标签(包括“div”、“td”、“tr”、“g”等)隐藏得更深一些,如下所示:
<tspan style="fill:#006600;font-size:7px;">CC002_33_N709_S503_C10</tspan>
其中“CC002_33_N709_S503_C10”是示例参考,“#006600”是指颜色。有(我认为)540行这样的。如果有人可以提供帮助,那真的很棒吗?非常感谢
根据 Uday 的回复进行编辑:
感谢您的建议,我已经在其中内置了“findAll”并使用索引来检索下一个片段。这个建议here 提到删除“tbody”标签,因为它可能不是源代码的一部分。只是添加'tspan'似乎并没有返回我需要的东西。这是我更新的代码:
for d in soup.find('div', attrs={'id':'wrapper'}).find(
'div', attrs={'class':'content'}).find(
'div', attrs={'id':'ResPanel'}).find(
'table', attrs={'id':'maintable'}).findAll(
'tr')[2].findAll('td')[0].find('div', attrs={'id':'sigheatmapcontainer'}):
print(d)
任何进一步的建议会真的很有帮助吗?
【问题讨论】:
-
错误
TypeError: 'NoneType' object is not iterable表示最终返回的对象 (find( 'tbody')) 不是运行 for 循环的列表。尝试使用find_all('tbody')获取表格内容。我想你可能需要find('tbody').find_all('tspan') -
代码运行没有崩溃到'sigheatmapcontainer',但似乎是空的,如果我尝试找到下一个div:.find('div', attrs={'id':'highcharts -40'}) 然后它返回 TypeError: 'NoneType' object is not iterable
标签: python beautifulsoup