python - 单击第一个链接 Mechanize python

标签 python url mechanize

我必须单击我找到的链接,但是当我运行该脚本时,它单击了第一个链接,我该如何修复它?
进口 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/

相关文章:

python - 使用模块名(字符串)调用模块的函数

url - 什么是标准的 Google Analytics UTM 归因方法?

java - 如何根据 URL 返回服务器的不同响应

url - 版本化 URL : how can I tell search engines about more recent versions of the current page?

python - 如何使用 Mechanize 从表的最后一列(该行包含某些单词)中提取 URL

Python/解析 : BeautifulSoup error "module obj is not callable" with results from Mechanize

python - 如何在 Scrapy 项目中使用 Privoxy 和 Tor

python sqlalchemy bulk_save_objects 不使用批量

ruby-on-rails - 在 Rails 中将 delayed_job 用于 Mechanize 脚本(NoMethodError : undefined method for nil:NilClass)

Python从末尾读取文件,文件很大,无法读入内存