使用beauthoulsoup查找特定标记

2024-09-28 20:45:33 发布

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

这是我正在分析的网站:http://uniapple.net/usaddress/address.php?address1=501+10th+ave&address2=&city=nyc&state=ny&zipcode=10036&country=US

我希望能够在td标签之间的第39行找到单词。这一行告诉我地址是住宅地址还是商业地址,这正是我编写脚本所需要的。在

这是我所拥有的,但是我得到了一个错误:

AttributeError: 'NoneType' object has no attribute 'find_next'

我使用的代码是:

^{pr2}$

Tags: httpcitynet网站address地址phpstate
3条回答

text参数在这种特殊情况下不起作用。这与如何计算元素的^{} property有关。相反,我将使用search function,在这里您可以实际调用get_text()并检查包含子节点的元素的完整“文本”:

label = thesoup.find(lambda tag: tag and tag.name == "th" and \
                                 "Residential" in tag.get_text())
comres = label.find_next("td").get_text()
print(str(comres))

打印Commercial。在

我们可以更进一步,创建一个可重用函数以通过标签获得值:

^{pr2}$

印刷品:

Commercial
NYC

你只缺一点家务活:

ths = thesoup.find_all("th")
for th in ths:
    if 'Residential or' in th.text:
        comres = th.find_next("td").text
        print(str(comres))
        >> Commercial

您需要使用正则表达式作为文本字段,如re.compile('Residential or'),而不是字符串。在

这对我很有用。我不得不反复查看提供的结果,但是如果您只希望每个页面都有一个结果,您可以将find替换为find_all

for r in thesoup.find_all(text=re.compile('Residential or')):
    r.find_next('td').text 

相关问题 更多 >