美丽的汤:替换返回的图像来源的一部分

时间:2018-10-19 22:45:08

标签: python beautifulsoup

我是python的新手,还是美丽的汤的新手,所以为了练习,我制作了一个在大多数情况下都可以工作的图像刮板。我能够找到图像并将其下载到我的计算机中,然后根据其名称将其放入文件夹中。但是我遇到了一个问题。这是我的代码

import requests
from bs4 import BeautifulSoup
import os.path

url = "https://example.net/g/1"
i = 1
data = requests.get(url)

soup = BeautifulSoup(data.text, 'html.parser')
for sou in soup.findAll("div", {"class": "gallery"}):
    sou.decompose()

containers = soup.find_all('img')
title = soup.find('h1').text
imgsrc = containers

for imgs in imgsrc: 
    if ".jpg" in imgs['src']:
        sauce = (imgs['src'])

        if sauce[:1] =="/":
          image = 'https:' + sauce
        else:
          image = sauce

        nametemp = imgs.get('alt')
        if nametemp is None:
                filename = str(i)
                i = i+1
                print(image)

运行此命令时,我会获得这些图像源,

  1. https://t.example.net/galleries/9/cover.jpg
  2. https://t.example.net/galleries/9/1t.jpg
  3. https://t.example.net/galleries/9/2t.jpg

这是我最想要的,但是返回的源是缩略图,所以它们很小。要获得完整尺寸的图像,这非常容易。只需更换两个T。

我的问题是我该如何替换上面的

  1. https://i.example.net/galleries/9/1.jpg
  2. https://i.example.net/galleries/9/2.jpg

我尝试使用replace_with()并查看了文档,但是我对它有所了解。

1 个答案:

答案 0 :(得分:0)

您的代码很乱,与您的问题无关。因此,假设您有一个名为thumbnails的URL列表:

thumbnails = [
    'https://t.example.net/galleries/9/1t.jpg',
    'https://t.example.net/galleries/9/2t.jpg',
    'https://t.example.net/galleries/9/3t.jpg',
]

然后,您可以在列表推导中使用正则表达式替换来按需转换URL:

import re
images = [re.sub(r't(\.jpg)', r'\1', url) for url in thumbnails]
相关问题