如何通过使用webscrapping查找图像链接

时间:2018-06-20 03:49:52

标签: python web-scraping

我想解析网页的图像链接。我尝试了以下代码,但显示出一些错误。

#!usr/bin/python
import requests
from bs4 import BeautifulSoup
url=raw_input("enter website")
r=requests.get("http://"+ url)
data=r.img
soup=BeautifulSoup(data)
for link in soup.find_all('img'):
    print link.get('src')

错误

File "img.py", line 6, in <module>
    data=r.img
AttributeError: 'Response' object has no attribute 'img'

2 个答案:

答案 0 :(得分:0)

您的错误是您想从img而不是Response那里获得source code

r=requests.get("http://"+ url)
# data=r.img # it is wrong

# change instead of `img` to `text`
data = r.text # here we need to get `text` from `Response` not `img`

# and the code
soup=BeautifulSoup(data)
for link in soup.find_all('img'):
    print link.get('src')

答案 1 :(得分:0)

下面您将找到包含import urllib.requestBeautifulSoup的有效版本:

import urllib.request
from bs4 import BeautifulSoup

url='http://python.org'
with urllib.request.urlopen(url) as response:
  html = response.read()

soup = BeautifulSoup(html, 'html.parser')

for link in soup.find_all('img'):
  print('relative img path')
  print(link['src'])
  print('absolute path')
  print(url + link['src'])

我希望这对您有所帮助:-)