【问题标题】:Beautifulsoup download all .zip files from Google Patent SearchBeautifulsoup 从 Google Patent Search 下载所有 .zip 文件
【发布时间】:2015-07-01 21:02:31
【问题描述】:

我正在尝试使用 Beautifulsoup 从 Google 专利档案中下载每个 zip 文件。以下是我迄今为止编写的代码。但似乎我无法将文件下载到桌面上的目录中。任何帮助将不胜感激

from bs4 import BeautifulSoup 
import urllib2
import re
import pandas as pd

url = 'http://www.google.com/googlebooks/uspto-patents-grants.html'

site = urllib2.urlopen(url)
html = site.read()
soup = BeautifulSoup(html)
soup.prettify()

path = open('/Users/username/Desktop/', "wb")

for name in soup.findAll('a', href=True):
    print name['href']
    linkpath = name['href']
    rq = urllib2.request(linkpath)
    res = urllib2.urlope

我应该得到的结果是,所有的 zip 文件都应该下载到特定的目录中。相反,我收到以下错误:

> #2015 --------------------------------------------------------------------------- AttributeError Traceback (most recent call last)
> <ipython-input-13-874f34e07473> in <module>() 17 print name['href'] 18
> linkpath = name['href'] ---> 19 rq = urllib2.request(namep) 20 res =
> urllib2.urlopen(rq) 21 path.write(res.read()) AttributeError: 'module'
> object has no attribute 'request' –

【问题讨论】:

  • 您遇到了什么问题?预期的结果是什么?会发生什么?
  • 它应该下载所有的 zip 文件,但我得到了这个错误。#2015 ------------------------ -------------------------------------------------- -- AttributeError Traceback (最近调用最后一次) in () 17 print name['href'] 18 linkpath = name['href'] ---> 19 rq = urllib2 .request(namep) 20 res = urllib2.urlopen(rq) 21 path.write(res.read()) AttributeError: 'module' object has no attribute 'request'

标签: python beautifulsoup web-crawler urllib2


【解决方案1】:

这是你的问题:

rq = urllib2.request(linkpath)

urllib2 是一个模块,其中没有 request 实体/属性。

我在 urllib2 中看到了一个 Request 类,但我不确定您是否打算实际使用它...

【讨论】:

    【解决方案2】:

    除了使用 urllib2 中不存在的 request 实体之外,您还没有正确输出到文件 - 您不能只打开目录,您必须分别打开每个文件进行输出。

    此外,'Requests' 包的界面比 urllib2 好得多。我建议安装它。

    请注意,无论如何,今天第一个 .zip 文件是 5.7Gb,因此流式传输到文件是必不可少的。

    真的,你想要更多这样的东西:

    from BeautifulSoup import BeautifulSoup
    import requests
    
    # point to output directory
    outpath = 'D:/patent_zips/'
    url = 'http://www.google.com/googlebooks/uspto-patents-grants.html'
    mbyte=1024*1024
    
    print 'Reading: ', url
    html = requests.get(url).text
    soup = BeautifulSoup(html)
    
    print 'Processing: ', url
    for name in soup.findAll('a', href=True):
        zipurl = name['href']
        if( zipurl.endswith('.zip') ):
            outfname = outpath + zipurl.split('/')[-1]
            r = requests.get(zipurl, stream=True)
            if( r.status_code == requests.codes.ok ) :
                fsize = int(r.headers['content-length'])
                print 'Downloading %s (%sMb)' % ( outfname, fsize/mbyte )
                with open(outfname, 'wb') as fd:
                    for chunk in r.iter_content(chunk_size=1024): # chuck size can be larger
                        if chunk: # ignore keep-alive requests
                            fd.write(chunk)
                    fd.close()
    

    【讨论】:

    • JohnH ...谢谢!这正是我正在寻找的。​​span>
    猜你喜欢
    • 2021-08-25
    • 1970-01-01
    • 2020-05-01
    • 2011-02-09
    • 1970-01-01
    • 2018-01-03
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多