我正在尝试单独打印图像Src标记值,我可以成功打印图像标记值,但无法获取src标记值。
import urllib3
import certifi
from urllib3 import PoolManager
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
manager=PoolManager(num_pools=3,cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
base_url="https://app.tipotapp.com/docs/quickstart/"
page=manager.request('GET',base_url)
soup = BeautifulSoup(page.data, 'html.parser')
idd='creating-an-application'
for sibling in soup.find(id=idd).next_siblings:
if sibling.name is None :
continue
elif sibling.name != 'h2' :
print(sibling.getText())
if sibling.img is not None:
print(sibling.img)
#print(sibling.select_one("img"))
else:
continue
else :
break
我现在得到的输出是,
打印: ....一些预期的字符串....然后下面的输出
<img alt="Student Management System"
src="https://app.tipotapp.com/docs/images/quickstart/image_004.png"/>
在那里,我想只打印src值。
答案 0 :(得分:1)
要获取属性的值,请使用__getitem__(self, key)
方法。
tag[key]
返回标记的'key'属性值,如果不存在则抛出异常。
只需将行print(sibling.img)
替换为
print(sibling.img['src'])
输出:
https://app.tipotapp.com/docs/images/quickstart/image_002.png
https://app.tipotapp.com/docs/images/quickstart/image_002_1.png
https://app.tipotapp.com/docs/images/quickstart/image_004.png