【问题标题】:Scraping multiple pages for parsing in Beautiful Soup在 Beautiful Soup 中抓取多个页面进行解析
【发布时间】:2011-11-30 23:57:38
【问题描述】:

我正在尝试从一个网站上抓取多个页面以供 BeautifulSoup 解析。到目前为止,我已经尝试使用 urllib2 来执行此操作,但遇到了一些问题。我尝试的是:

import urllib2,sys
from BeautifulSoup import BeautifulSoup

for numb in ('85753', '87433'):
    address = ('http://www.presidency.ucsb.edu/ws/index.php?pid=' + numb)
html = urllib2.urlopen(address).read()
soup = BeautifulSoup(html)

title = soup.find("span", {"class":"paperstitle"})
date = soup.find("span", {"class":"docdate"})
span = soup.find("span", {"class":"displaytext"})  # span.string gives you the first bit
paras = [x for x in span.findAllNext("p")]

first = title.string
second = date.string
start = span.string
middle = "\n\n".join(["".join(x.findAll(text=True)) for x in paras[:-1]])
last = paras[-1].contents[0]

print "%s\n\n%s\n\n%s\n\n%s\n\n%s" % (first, second, start, middle, last)

这只会给我numb 序列中第二个数字的结果,即http://www.presidency.ucsb.edu/ws/index.php?pid=87433。我也尝试过使用机械化,但没有成功。理想情况下,我想做的是有一个带有链接列表的页面,然后自动选择一个链接,将 HTML 传递给 BeautifulSoup,然后移动到列表中的下一个链接。

【问题讨论】:

    标签: python web-scraping urllib2


    【解决方案1】:

    您需要将其余代码放入循环中。现在您正在迭代元组中的两个项目,但在迭代结束时,只有最后一个项目仍然分配给 address,随后在循环外解析。

    【讨论】:

    • 这就是问题所在。非常感谢。
    【解决方案2】:

    我认为您只是错过了循环中的缩进:

    import urllib2,sys
    from BeautifulSoup import BeautifulSoup
    
    for numb in ('85753', '87433'):
        address = ('http://www.presidency.ucsb.edu/ws/index.php?pid=' + numb)
        html = urllib2.urlopen(address).read()
        soup = BeautifulSoup(html)
    
        title = soup.find("span", {"class":"paperstitle"})
        date = soup.find("span", {"class":"docdate"})
        span = soup.find("span", {"class":"displaytext"})  # span.string gives you the first bit
        paras = [x for x in span.findAllNext("p")]
    
        first = title.string
        second = date.string
        start = span.string
        middle = "\n\n".join(["".join(x.findAll(text=True)) for x in paras[:-1]])
        last = paras[-1].contents[0]
    
        print "%s\n\n%s\n\n%s\n\n%s\n\n%s" % (first, second, start, middle, last)
    

    我认为这应该可以解决问题..

    【讨论】:

    • 这就是问题所在。上面的答案指出了这一点。非常感谢您的帮助。
    【解决方案3】:

    这是一个更简洁的解决方案(使用 lxml):

    import lxml.html as lh
    
    root_url = 'http://www.presidency.ucsb.edu/ws/index.php?pid='
    page_ids = ['85753', '87433']
    
    def scrape_page(page_id):
        url = root_url + page_id
        tree = lh.parse(url)
    
        title = tree.xpath("//span[@class='paperstitle']")[0].text
        date = tree.xpath("//span[@class='docdate']")[0].text
        text = tree.xpath("//span[@class='displaytext']")[0].text_content()
    
        return title, date, text
    
    if __name__ == '__main__':
        for page_id in page_ids:
            title, date, text = scrape_page(page_id)
    

    【讨论】:

    • 谢谢。我实际上比 BeautifulSoup 方法更喜欢这个。
    • 我喜欢这个解决方案。您将如何保存正在抓取的页面?
    • @Joe 应该像这里的第三个示例一样简单:docs.python.org/2/library/csv.html#examples
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2021-01-26
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2020-12-01
    相关资源
    最近更新 更多