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'])
我希望这能帮助您:-)什么是错误,哪行返回错误?