-1

为什么我从这里得到的下面的代码:

http://blog.yhathq.com/posts/image-classification-in-Python.html

不给结果。一切似乎都很好。它运行没有错误,但我没有得到任何图像。我究竟做错了什么 ?

from bs4 import BeautifulSoup
import requests
import re
import urllib2
import os


def get_soup(url):
    return BeautifulSoup(requests.get(url).text)

image_type = "check"
query = "check"
url = "http://www.bing.com/images/search?q=" + query + \ 
    "&qft=+filterui:color2-bw+filterui:imagesize-large&FORM=R5IR3"

soup = get_soup(url)
images = [a['src'] for a in soup.find_all("img", {"src": re.compile("mm.bing.net")})]

for img in images:
    raw_img = urllib2.urlopen(img).read()
    cntr = len([i for i in os.listdir("images") if image_type in i]) + 1
    f = open("images/" + image_type + "_"+ str(cntr), 'wb')
    f.write(raw_img)
    f.close()
4

2 回答 2

0

attr“src”应该是“src2”。试试这个代码。

images = [a['src2'] for a in soup.find_all("img", {"src2": re.compile("mm.bing.net")})]
于 2014-01-19T08:27:39.960 回答
0

我得到了一个SyntaxError并将url行更改为:

url = ("http://www.bing.com/images/search?q=" + query +
       "&qft=+filterui:color2-bw+filterui:imagesize-large&FORM=R5IR3")

然后它运行良好。

于 2013-06-13T15:39:44.287 回答