【问题标题】:Following links with Mechanize跟随机械化的链接
【发布时间】:2013-01-07 04:53:14
【问题描述】:

我想使用 Mechanize python 库来跟踪网站中的某些链接,但我感兴趣的唯一链接是 <div> 标记中的链接。 This 问题是相关的,但他们使用 lxml 解析器实现它,我不熟悉,我更喜欢使用 BeautifulSoup。

我已经使用 BeautifulSoup 找到了相关链接,但我不知道如何使用 Mechanize(或其他东西)来跟踪这些链接。有没有办法将字符串传递给 Mechanize 以便它跟随它?

【问题讨论】:

    标签: python web-scraping beautifulsoup mechanize


    【解决方案1】:

    简单的open() 就足够了:

    br.open('http://google.com')
    

    【讨论】:

    • 这比我想象的要容易得多!
    【解决方案2】:
    import mechanize
    response = mechanize.urlopen("http://example.com/")
    content = response.read() #The content is the code of the page (html)
    

    或者,如果您想添加诸如标题之类的内容:

    import mechanize
    request = mechanize.Request("http://example.com/")
    response = mechanize.urlopen(request)
    content = response.read() #The content is the code of the page (html)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多