如何从抓取的链接中删除后缀?

How to remove suffix from scraped links?

我正在寻找从网站获取全尺寸图像的解决方案。

通过使用我最近通过 Whosebug 上某人的帮助完成的代码,我能够下载全尺寸图像和缩小尺寸的图像。

我想要的是所有下载的图片都是全尺寸的。

例如,有些图片文件名有“-625x417.jpg”作为后缀,有些图片没有。

https://www.bikeexif.com/1968-harley-davidson-shovelhead (has suffix) https://www.bikeexif.com/harley-panhead-walt-siegl (None suffix)

如果可以去掉这个后缀,那就是全尺寸图片了。

https://kickstart.bikeexif.com/wp-content/uploads/2018/01/1968-harley-davidson-shovelhead-625x417.jpg (Scraped) https://kickstart.bikeexif.com/wp-content/uploads/2018/01/1968-harley-davidson-shovelhead.jpg (Full-size image's filename if removed: -625x417)

考虑到文件名可能存在不同的图像分辨率, 所以它也需要以不同的尺寸移除。

我想我可能需要使用正则表达式从下面过滤掉'- 3digit x 3digit'。

但我真的不知道该怎么做。

如果你能做到,请帮我完成这个。谢谢!

images_url = selector_article.xpath('//div[@id="content"]//img/@src').getall() + \
             selector_article.xpath('//div[@id="content"]//img/@data-src').getall()

完整代码:

import requests
import parsel
import os

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

for page in range(1, 310):
    print(f'======= Scraping data from page {page} =======')

    url = f'https://www.bikeexif.com/page/{page}'

    response = requests.get(url, headers=headers)
    selector = parsel.Selector(response.text)

    containers = selector.xpath('//div[@class="container"]/div/article[@class="smallhalf"]')

    for v in containers:

        old_title = v.xpath('.//div[2]/h2/a/text()').get()
        
        if old_title is not None:
            title = old_title.replace(':', ' -').replace('?', '')

        title_url = v.xpath('.//div[2]/h2/a/@href').get()
        print(title, title_url)

        os.makedirs( os.path.join('bikeexif', title), exist_ok=True )

        response_article = requests.get(url=title_url, headers=headers)
        selector_article = parsel.Selector(response_article.text)

        # Need to get full-size images only
        # (* remove if suffix exist, such as -625x417, if different size of suffix exist, also need to remove)
        images_url = selector_article.xpath('//div[@id="content"]//img/@src').getall() + \
                    selector_article.xpath('//div[@id="content"]//img/@data-src').getall()
        print('len(images_url):', len(images_url))

        for img_url in images_url:

            response_image = requests.get(url=img_url, headers=headers)

            filename = img_url.split('/')[-1]
            
            with open( os.path.join('bikeexif', title, filename), 'wb') as f:
                f.write(response_image.content)
                print('Download complete!!:', filename)

我会选择这样的东西:

import re

url = 'https://kickstart.bikeexif.com/wp-content/uploads/2018/01/1968-harley-davidson-shovelhead-625x417.jpg'

new_url = re.sub('(.*)-\d+x\d+(\.jpg)', r'', url)
#https://kickstart.bikeexif.com/wp-content/uploads/2018/01/1968-harley-davidson-shovelhead.jpg

解释(另见 here):

  • 正则表达式分为三个部分:(.*)基本上是指任意长度的任意一组字符,括号将它们组合在一起。
  • -\d+x\d+ 表示破折号,后跟一位或多位数字,然后是 x 后跟一位或多位数字。
  • 最后一部分就是 .jpg,我们使用 \ 因为 . 是一个带有正则表达式的特殊字符,所以斜线转义知道我们的意思是 . 而不是“0 个或更多”

re.sub 的第二部分我们有 意思是“第一部分第一组括号中的任何内容”和“第二部分第二组括号中的任何内容”第一部分。

最后,最后一部分就是您要解析的字符串。