我对 BeautifulSoup
还很陌生。我正在尝试打印来自 http://www.bing.com/images?q=owl 的图像链接:
redditFile = urllib2.urlopen("http://www.bing.com/images?q=owl")
redditHtml = redditFile.read()
redditFile.close()
soup = BeautifulSoup(redditHtml)
productDivs = soup.findAll('div', attrs={'class' : 'dg_u'})
for div in productDivs:
print div.find('a')['t1'] #works fine
print div.find('img')['src'] #This getting issue KeyError: 'src'
但这仅给出标题,而不给出图像源 有什么问题吗?
编辑: 我已经编辑了我的源,仍然无法获取图像网址。
最佳答案
Bing 正在使用一些技术来阻止自动抓取工具。我尝试打印
div.find('img')
发现他们正在属性名称 src2 中发送源代码,因此以下应该可以工作 -
div.find('img')['src2']
这对我有用。希望对您有所帮助。
关于python - 从页面获取特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731895/