我只想下载python中的.html文件。 代码:
import urllib2
hdr = {'User-Agent': 'Mozilla/5.0'}
urls=['http://www.nydailynews.com/sports/soccer-fans-stampede-south-african-stadium-nigeria-north-korea-world-cup-warmup-article-1.179211']
path='C:/Users/sony/Desktop/Python'
for i,site in enumerate(urls):
print (site)
req = urllib2.Request(site, headers=hdr)
page = urllib2.build_opener(urllib2.HTTPCookieProcessor).open(req)
page_content = page.read()
with open(path+'/'+str(i)+'.html', 'w') as fid:
fid.write(page_content)
但这有时会产生这种输出 https://drive.google.com/file/d/0B16PrXUjs69zWFJvWmJ6aFhyN0k/view?usp=sharing 我一点也不明白。 我用python中的goose读取这个文件,当我读取这样一个文件时,它什么也没有显示。你知道吗
使用requests为您完成所有工作,使用
.content
让请求处理编码:输出:
如果要多次尝试相同的url,可以使用try/除了捕获
requests.ConnectionError
:通过查看响应头:
我看到内容是gzip,尝试使用zlib模块来解压数据。你知道吗
要检查数据是否已压缩,请添加以下行:
请阅读this来举例说明如何解压身体。你知道吗
相关问题 更多 >
编程相关推荐