使用Python / urllib / beautifulsoup从URL批量下载文本和图像?
发布时间:2020-12-20 13:24:56 所属栏目:Python 来源:网络整理
导读:我一直在浏览这里的几篇帖子,但我无法用 Python从批量下载图片和文本来给定URL. import urllib,urllib2import urlparsefrom BeautifulSoup import BeautifulSoupimport os,sysdef getAllImages(url): query = urllib2.Request(url) user_agent = "Mozilla/4.
我一直在浏览这里的几篇帖子,但我无法用
Python从批量下载图片和文本来给定URL.
import urllib,urllib2 import urlparse from BeautifulSoup import BeautifulSoup import os,sys def getAllImages(url): query = urllib2.Request(url) user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 1.0.3705)" query.add_header("User-Agent",user_agent) page = BeautifulSoup(urllib2.urlopen(query)) for div in page.findAll("div",{"class": "thumbnail"}): print "found thumbnail" for img in div.findAll("img"): print "found image" src = img["src"] if src: src = absolutize(src,pageurl) f = open(src,'wb') f.write(urllib.urlopen(src).read()) f.close() for h5 in div.findAll("h5"): print "found Headline" value = (h5.contents[0]) print >> headlines.txt,value def main(): getAllImages("http://www.nytimes.com/") 以上是一些更新的代码.会发生什么,什么都不是.代码没有找到任何带有缩略图的div,显然,没有任何结果打印….所以可能我错过了一些指向获取包含图像和标题的正确div? 非常感谢! 解决方法
您正在使用的操作系统不知道如何写入您在src中传递它的文件路径.确保用于将文件保存到磁盘的名称是操作系统实际可以使用的名称:
src = "abc.com/alpha/beta/charlie.jpg" with open(src,"wb") as f: # IOError - cannot open file abc.com/alpha/beta/charlie.jpg src = "alpha/beta/charlie.jpg" os.makedirs(os.path.dirname(src)) with open(src,"wb" as f: # Golden - write file here 一切都会开始奏效. 还有一些额外的想法: >确保规范化保存文件路径(例如os.path.join(some_root_dir,* relative_file_path *)) – 否则您将根据其src在整个硬盘上写入图像.>除非您正在运行某种类型的测试,否则最好在您的user_agent字符串中宣传您是一个机器人并且尊重robots.txt文件(或者,提供某种联系信息以便人们可以要求您在需要时停止). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |