我想解析网页的图像链接。我尝试了以下代码,但显示出一些错误。
#!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'
答案 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.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'])
我希望这对您有所帮助:-)