我不熟悉使用 bs4 进行网络抓取,我想从蛋白质数据库 (PDB) 获取蛋白质图像:
https://www.rcsb.org/structure/1A69
当我使用 Chrome Inspector 检查 HTML 时,我看到图像是通过一个 http 链接获取的,我可以轻松访问该链接并从中保存图像。
<img class="img-responsive center-block mainImage"
src="https://cdn.rcsb.org/images/rutgers/a6/1a69/1a69.pdb1-500.jpg">
但是,当我运行我的脚本来提取 src 时,我只得到它作为 base64。
data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=
我做错了什么吗?发生了什么?有没有办法从 base64 获取 http 链接?
我的代码:
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen
url = "https://www.rcsb.org/structure/1A69"
resp = urlopen(url)
page = bs(resp,"html.parser")
for img in page.findAll('img',{'class':'img-responsive'}):
src = img['src']
print(src)
最佳答案
图像 URL 由 Javascript 动态组成,但您可以使用此 Python 脚本模拟组成:
import requests
from bs4 import BeautifulSoup
url = 'https://www.rcsb.org/structure/1A69'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
pdb_id = url.split('/')[-1].lower()
images_location = "https://cdn.rcsb.org/images/rutgers/"
num_items = len( soup.select('#carousel-structuregallery .item') )
pdb_hash = pdb_id[1:3]
# print image urls to screen:
for i in range(num_items):
# 0 = Asymmetric; 1+ = Biological Assembly
if i == 0:
img_url = images_location + pdb_hash + '/' + pdb_id + '/' + pdb_id + '.pdb-500.jpg'
else:
img_url = images_location + pdb_hash + '/' + pdb_id + '/' + pdb_id + '.pdb' + str(i) + '-500.jpg'
print(img_url)
打印:
https://cdn.rcsb.org/images/rutgers/a6/1a69/1a69.pdb-500.jpg
https://cdn.rcsb.org/images/rutgers/a6/1a69/1a69.pdb1-500.jpg
https://stackoverflow.com/questions/61633127/