我一直在浏览这里的几篇帖子,但我无法用
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?
非常感谢!