gpt4 book ai didi

python - Beautifulsoup 从 Google 专利搜索下载所有 .zip 文件

转载 作者:行者123 更新时间:2023-12-01 04:44:20 27 4
gpt4 key购买 nike

我想做的是使用 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' –

最佳答案

除了使用 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()

关于python - Beautifulsoup 从 Google 专利搜索下载所有 .zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827479/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com