我试着从这张表格的“工业”行中刮出文字:
<tr>
<th style="padding-right: 0.5em;" scope="row">Industry</th>
<td class="category" style="line-height: 1.35em;">
<a title="Professional Services" href="/wiki/Professional_services">Professional services</a>
<br></br>
<a title="Technology Services" href="/wiki/Technology_services">Technology services</a>
</td>
</tr>我的python代码如下(r是表变量):
industry = r.find('th', text = 'Industry').findNext('td').find_all('a')[0].get_text() print industry
第一个“专业服务”被打印出来,但是我得到了错误:
IndexError:列出超出范围的索引
发布于 2015-06-26 15:08:55
据我所能复制的,这可能是因为解析器之间的差异
>>> soup = BeautifulSoup(data, "html.parser")
>>> soup.find('th', text='Industry').findNext('td').find_all('a')[0].get_text()
u'Professional services'
>>>
>>> soup = BeautifulSoup(data, "lxml")
>>> soup.find('th', text='Industry').findNext('td').find_all('a')[0].get_text()
u'Professional services'
>>>
>>> soup = BeautifulSoup(data, "html5lib")
>>> soup.find('th', text='Industry').findNext('td').find_all('a')[0].get_text()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'findNext'请注意,我无法复制与您所得到的完全相同的错误。
https://stackoverflow.com/questions/31076476
复制相似问题