【问题标题】:Mechanize: Follow links inside a div机械化:跟随 div 内的链接
【发布时间】:2012-07-14 04:36:35
【问题描述】:

让 Mechanize 跟随位于某个 div 内的链接 (br.follow_link) 的最 Pythonic 方式是什么?我知道如何在 BeautifulSoup 的帮助下做到这一点,但是有没有办法通过 Mechanize 做到这一点?

示例 div:

<div id="blah_links">
 <a href="LINK1" class="active">1</a> |
 <a href="LINK2">2</a> |
 <a href="LINK3">3</a> |
 <a href="LINK4">NEXT</a>
</div>

【问题讨论】:

    标签: python html mechanize


    【解决方案1】:

    我最近遇到了类似的问题,这就是我所做的

    url = "www.somewhere.com"
    br = mechanize.Browser()
    br.open(url)
    
    encoded_data = UnicodeDammit(br.response().read(),isHTML=True).unicode
    parser = lxml_html.fromstring(encoded_data)
    
    soup_xpath = "//div[@id='BODYCON']//a/@href"
    valid_links = soup.xpath(soup_xpath)
    links  = [ link for link if link.url in valid_links ] 
    

    【讨论】:

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