如何使用mechanize找到具有特定id-tag的链接对象?

问题描述 投票:0回答:1

我正在编写一个Web抓取脚本,在某些时候应该单击具有特定id-tag的锚链接。我可以通过使用BeautifulSoup找到链接,但是我找不到使用mechanize获取mechanize.Link对象的方法。

到目前为止这是我的代码。

import mechanize

br = mechanize.Browser()
response = br.open("myUrl")

for link in br.links():
    if str(link.attrs["id"]) == "cell_14_2":
        click_link(link)
    break

我期待找到ID为“cell_14_2”的链接对象,但我收到一条错误消息:

if str(link.attrs["id"]) == "cell_14_2":

消息是:

TypeError: list indices must be integers, not str

如何找到mechanize.Link对象并单击它?

python html hyperlink mechanize
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.