英国标准4儿童标签[2] 提供“listiterator对象没有属性”\uu getitem\uu

2024-04-20 07:57:24 发布

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

我有以下代码(Python 2.7,bs4)可以工作:

html = urllib2.urlopen("https://www.zidisha.org/microfinance/loan/youmpi/1434.html").read())
soup=BeautifulSoup(html)
tag = soup.find(text=re.compile("On-Time Repayments:")).find_parent("td").find_next_sibling("td")

print type(tag)
for child in tag.children:
print repr(child)

#Output:
<class 'bs4.element.Tag'>
u'\n'
u'modified by Julia to add number of months repayments were due 15-10-2013'
u'\n\n80% (10)\n\n' 

我想得到标记中的第三个元素--“80%(10)”——(从unicode中剥离和转换没有问题),但是当我尝试定义: myVar=儿童标签[2] ,我得到以下错误: 'listiterator对象没有属性'getitem'

我使用以下方法进行了破解修复: myVar=tag.next_元素.下一个_元素。下一个元素.strip(),但感觉我的IDE在评判我。在

我以为我可以通过列表理解来迭代任何东西,我可以用索引来获取特定元素,但显然不是。什么是最好的方法来获得标签的第三个(或理论上的第20个)元素而不进行链接。下一个元素方法是什么?在


Tags: 方法代码child元素htmltag标签find