Beautifulsoup 从 Google 专利搜索下载所有 .zip 文件
Beautifulsoup download all .zip files from Google Patent Search
我想做的是使用 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' –
这是你的问题:
rq = urllib2.request(linkpath)
urllib2 是一个模块,里面没有 request entity/attribute。
我在 urllib2 中看到 Request class,但我不确定这是否是您实际打算使用的...
除了使用来自 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()
我想做的是使用 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' –
这是你的问题:
rq = urllib2.request(linkpath)
urllib2 是一个模块,里面没有 request entity/attribute。
我在 urllib2 中看到 Request class,但我不确定这是否是您实际打算使用的...
除了使用来自 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()