我想解析网页的图像链接。我已经尝试了下面的代码,但它显示了一些错误。
#!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'
你的错误是你想从Response
获取img
,而不是从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')
下面您将找到带有import urllib.request
和BeautifulSoup
的工作版本:
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'])
我希望这对你有所帮助:-(