[英]Python : extracting image source from HTML tag using Beautiful Soup
我试图单独打印图像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值。
要获取属性的值,请使用__getitem__(self, key)
方法。
tag[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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.