gpt4 book ai didi

python - 单击第一个链接 Mechanize python

转载 作者:行者123 更新时间:2023-12-04 16:22:41 25 4
gpt4 key购买 nike

我必须单击我找到的链接,但是当我运行该脚本时,它单击了第一个链接,我该如何修复它?
进口 Mechanize

br = mechanize.Browser()

br.addheaders = [("用户代理","Chrome")]

br.set_handle_robots(假)

而真:

kaynak = br.open("https://temp-mail.org/en/option/change/")

br.select_form(nr=0)

email = br.form.find_control(id='mail', name='mail')

email.value = "something"

submit = br.submit(nr=0)

go = br.open("https://temp-mail.org/en/option/refresh/")

target_text = 'Facebook'

for link in br.links():
print(link)
print(link.text)

if link.text == target_text:
print('match found')
break

br.follow_link(link)
print(br.geturl())

最佳答案

您的缩进已关闭。您的 for 循环现在遵循它找到的每个链接,而不仅仅是匹配的链接。要么将您的关注语句放在 if 中之前 break , 或在 for 循环之后:

for link in br.links():
print(link)
print(link.text)

if link.text == target_text:
print('match found')
# Here...
br.follow_link(link)
print(br.geturl())
break

# Or here...
br.follow_link(link)
print(br.geturl())

关于python - 单击第一个链接 Mechanize python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com