Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用webscraping查找图像链接_Python_Web Scraping - Fatal编程技术网

Python 如何使用webscraping查找图像链接

Python 如何使用webscraping查找图像链接,python,web-scraping,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('s

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

#!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'
文件“img.py”,第6行,在
数据=r.img
AttributeError:“Response”对象没有属性“img”

您的错误是希望从
响应
获取
img
,而不是从
源代码

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')

在下面,您将找到一个具有
导入urllib.request
美化组
的工作版本:

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'])

我希望这能帮助您:-)

什么是错误,哪行返回错误?