【问题标题】:Iterate through a URL to save an unknown number of pictures遍历一个URL保存未知数量的图片
【发布时间】:2021-03-13 11:00:20
【问题描述】:

我正在尝试从同行评审网站上抓取多张图片以进行一些基于图像的分析,但我无法确定是否可以更改 URL 以从特定论文中抓取每张图片。全文在这里:https://www.biorxiv.org/content/10.1101/716043v1.full

我使用的图片的 URL 是:https://www.biorxiv.org/content/biorxiv/early/2019/07/26/716043/F1.large.jpg?width=800&height=600&carousel=1。 'F1.large'指的是'图1',共有6个数字。有没有办法可以增加“F1”,以便它将所有数字收集到单独的 jpg 文件中?我也希望它适用于任意数量的数字,因为并非每篇论文都会有 6 个,有些会更多,有些会更少。

非常感谢任何帮助。

import urllib.request

url = "https://www.biorxiv.org/content/biorxiv/early/2019/07/26/716043/F2.large.jpg?width=800&height=600&carousel=1"
picture = urllib.request.urlretrieve(url, "f2.jpg")

【问题讨论】:

    标签: python url web-scraping


    【解决方案1】:

    您可以遍历包含图像的标签。特别是带有class="fig-inline-img" 的<div> 标签。可以抓取alt属性参考图

    代码:

    import requests
    from bs4 import BeautifulSoup
    import urllib.request
    
    
    url = 'https://www.biorxiv.org/content/10.1101/716043v1.full'
    
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    images = soup.find_all('div',{'class':'fig-inline-img'})
    for each in images:
        fig = '_'.join(each.find('img')['alt'].replace('.','').split())
        img_link = each.find('a')['href']
        picture = urllib.request.urlretrieve(img_link, "{fig}.jpg".format(fig=fig))
        print ('Saved: {fig}'.format(fig=fig))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2016-08-27
      相关资源
      最近更新 更多