【问题标题】:How to get large amounts of href links of very large contents of website with Beautifulsoup如何使用 Beautifulsoup 获取非常大的网站内容的大量 href 链接
【发布时间】:2013-02-14 01:51:27
【问题描述】:

我正在解析一个包含 1000 多个 href 链接的大型 html 网站。我正在使用 Beautifulsoup 来获取所有链接,但是当我再次运行程序时,beautifulsoup 无法处理它。 (查找特定的所有'td'标签。我将如何克服这个问题?虽然我可以使用urllib加载html页面,但无法打印所有链接。当我将它与find一个'td'标签一起使用时,它通过了。

Tag = self.__Page.find('table', {'class':'RSLTS'}).findAll('td')    
    print Tag           

    for a in Tag.find('a', href= True):
        print "found", a['href']

现在工作

Tag = self.__Page.find('table', {'class':'RSLTS'}).find('td')
打印标签

    for a in Tag.find('a', href= True):
        print "found", a['href']

【问题讨论】:

    标签: python-2.7 beautifulsoup urllib


    【解决方案1】:

    您需要遍历它们:

    tds = self.__Page.find('table', class_='RSLTS').find_all('td')
    
    for td in tds:
        a = td.find('a', href=True)
    
        if a:
            print "found", a['href']
    

    虽然如果你有很多东西我只会使用 lxml:

    root.xpath('table[contains(@class, "RSLTS")]/td/a/@href')
    

    【讨论】:

    • tds = self.__Page.find('table', class_='RSLTS').find_all('td') 由于超过 1000 个“td”而无法正常工作。我尝试安装 lxml 但无法导入 lxml.html.fromstring。现在我正在尝试删除我之前安装的 lxml。 Python 库很难在 Windows 上安装。
    • 当程序到达第一行'findall('td')'时,它挂起。我再也看不到任何结果了。
    • 我得到了解决方案,因为 __init__(self) 的构造函数不包含来自 beautifulsoup 的整个标签。
    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多