我必须单击我找到的链接,但是当我运行该脚本时,它会点击第一个链接,我该如何修复它? 进口机械化
br = mechanize.Browser()
br.addheaders = [(“用户代理”,“Chrome”)]
br.set_handle_robots(假)
而True:
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())
答案 0 :(得分:0)
你的诅咒已经关闭。您的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())