python - BeautifulSoup img src 获取 base64 而不是实际链接

我不熟悉使用 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/

相关文章:

ios - 在 iOS 13 中获取 Root View Controller 的正确方法是什么?

python - 逐步将新数据附加并绘制到 matplotlib 行

python - 你如何使用 python-rtmidi 获取 midi 事件

python - 包含 json 格式列的 Dask 数据框

typescript - 在字符串值成员中使用枚举

java - Netbeans 中的 JUnit 5 测试

html - 如何在没有 JavaScript 的情况下设置 CSS 动画速度?

kubernetes - 无法从普罗米修斯适配器检索自定义指标

google-app-engine - 如何使用 Google Cloud Tasks 扩展拉取队列

reactjs - 类型 'typeof class' 不可分配给类型 'ComponentType