python如何使用request爬取图片

本文介绍了一种使用Python实现的网页图片爬虫,通过requests和lxml.etree库抓取指定网站上的图片链接,并将其保存到本地目录。文章提供了详细的代码实现,包括获取图片列表、解析图片URL和下载图片到本地的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是代码的简单实现,变量名和方法都是跑起来就行,没有整理,有需要的可以自己整理下:

image2local:

import requests
import time
from lxml import etree
import os

#存储位置
dir = 'xxxxxx'

#网址地址
image_host = 'https://www.27270.com'

#获取爬取列表
def get_list(page_detail=''):
    #爬取列表
    page = requests.get('https://www.27270.com/ent/meinvtupian/list_11_{0}.html'.format(page_detail))
    #解析列表数据
    image_urls = etree.HTML(page.text)
    print(image_urls.xpath('/html/body/div[2]/div[7]/ul/li/a[2]/@href'))
    pages = image_urls.xpath('/html/body/div[2]/div[7]/ul/li/a[2]/@href')

    return pages

def getEntityUrl(url):
    #爬取传过来的地址
    page = requests.get(image_host+url)
    page.keep_alive = False
    image_urls = etree.HTML(page.content)

    try:
        image = image_urls.xpath('//*[@id="picBody"]/p/a[1]/img/@src')[0]
        next = image_urls.xpath('//*[@id="nl"]/a/@href')[0]
        title = image_urls.xpath('/html/body/div[2]/div[2]/h1/text()')[0]
    except:
        return False

    #爬取第一张
    if next.find('##')>1:
        return False
    else:
        result = image2local(image,title,next)
    return True

#爬取图片
def image2local(url,title,name):
    title = title.split('(')[0]
    if not os.path.exists(dir + title):
        os.mkdir(dir + title)
    try:
        image = requests.get(url)
    except:
        return True
    # print(image.content+)
    with open(dir + title+'/{0}.jpg'.format(name),'wb') as f:
        f.write(image.content)
        f.close()
    return True

if __name__ == '__main__':
    x = range(1,215)
    for i in x:
        list = get_list(i)
        for image in list:
            num = 1
            result = True
            while result:
                next = image.replace('.','_{0}'.format(num)+'.')
                num = num+1
                result = getEntityUrl(next)

python代码是现学现写的,大家勿喷

转载于:https://www.cnblogs.com/jack-jt-z/p/10474208.html

使用Pythonrequests库爬取百度图片,您可以按照以下步骤进行操作: 1. 安装requests库:如果您还没有安装requests库,可以使用以下命令进行安装: ``` pip install requests ``` 2. 编写爬取代码:下面是一个简单的例子,演示如何使用requests库爬取百度图片的搜索结果: ```python import requests def download_images(keyword, num_images): # 构建百度图片搜索的URL search_url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&word={keyword}&pn=0&rn={num_images}' # 发送GET请求 response = requests.get(search_url) # 处理响应结果 if response.status_code == 200: # 获取图片URL image_urls = [data['middleURL'] for data in response.json().get('data', []) if 'middleURL' in data] # 下载图片 for i, url in enumerate(image_urls): try: response = requests.get(url) with open(f'image{i}.jpg', 'wb') as f: f.write(response.content) print(f'Successfully downloaded image{i}.jpg') except: print(f'Failed to download image{i}.jpg') else: print('请求失败') # 要搜索的关键词和要下载的图片数量 keyword = 'cat' num_images = 10 # 执行爬取 download_images(keyword, num_images) ``` 在上述代码中,我们构建了一个特定的URL,其中包含了搜索关键词和要下载的图片数量。然后,我们发送GET请求来获取搜索结果,并解析响应结果中的图片URL。最后,我们使用requests库下载图片并保存到本地。 请注意,这只是一个简单的示例,实际应用中可能需要更多的处理和错误处理。另外,爬取网站的图片可能需要遵守该网站的使用条款和法律法规。请确保遵守相关规定并尊重他人的权益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值