Python中的图像抓取程序无法按预期运行

前端之家收集整理的这篇文章主要介绍了Python中的图像抓取程序无法按预期运行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的代码只返回一个空字符串,我不知道为什么.

  1. import urllib2
  2. def getImage(url):
  3. page = urllib2.urlopen(url)
  4. page = page.read() #Gives HTML to parse
  5. start = page.find('

它只返回它找到的第一个图像,所以它不是一个非常好的图像刮刀;那说,我现在的主要目标只是为了能够找到一个图像.我无能为力.

最佳答案
考虑使用BeautifulSoup来解析HTML:

  1. from BeautifulSoup import BeautifulSoup
  2. import urllib
  3. url = 'http://www.google.com'
  4. html = urllib.urlopen(url).read()
  5. soup = BeautifulSoup(html)
  6. for img in soup.findAll('img'):
  7. print img['src']

猜你在找的Python相关文章