爬虫实例图片爬取--1(某领域)

爬图片的流程

1.向目标网站发送请求

2.获取数据(网页源码)

3.解析数据

4.向详情页发送请求

5.获取数据

确定网址,发起请求

导入请求库,然后发起请求

import requests

headers = {
    'User-Agent': 'sadasdsafdgdsd'
}
url = 'https://www.xxxxx.com/104212.html'
response = requests.get(url=url,headers=headers)
print(response.text)

接着导入解析库

import parsel

对数据进行解析

import requests
import parsel

headers = {
    'User-Agent': 'sadasdsafdgdsd'
}
url = 'https://www.jdlingyu.com/104212.html'
response = requests.get(url=url,headers=headers)
html = response.text
select = parsel.Selector(html)
print(select)

利用css来进行定位,

img_all = select.css('.entry-content img::attr(src)').getall()

 ::attr(src)是提取这元素中的src对应的地址。

但是提取出来并不好看,可以用for来进行再次提取

for img in img_all:
    print(img)

既然拿到了图片的地址,那么就对这个地址发起请求

img_data = requests.get(img,headers=headers).content

 然后取这个地址的反斜杠最后一段,用来做图片的名称

img_name = img.split('/')[-1]

接着创建一个名为img的文件夹用来存图片

    with open(f'img/{img_name}',mode='wb') as f:
        f.write(img_data)

可以了,但是就这四张太少了,来整多点!!

那么重新来一遍,对整体进行请求

import requests
import parsel

headers = {
    'User-Agent': 'sadasdsafdgdsd'
}
url = 'https://www.xxxx.com/tuji'
response = requests.get(url=url,headers=headers)
html = response.text
select = parsel.Selector(html)
print(select)

 

这里有一堆

再次用css来进行定位

 因为它的地址和名称都在这,所以

title_list = select.css('.post-info h2 a::text').getall()
link_list = select.css('.post-info h2 a::attr(href)').getall()

然后

for title,link in zip(title_list,link_list):

把它们提取出来。

接着导入os模块,来创建文件夹

    if not os.path.exists(f'img/{title}'):
        os.mkdir(f'img/{title}')

然后运行就完事了

完整代码如下:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :untitled1 
@File    :实例1.py
@IDE     :PyCharm 
@Author  :冷巷(✘_✘)
@Date    :2022/7/15 14:28 
"""

import requests
import parsel
import os

headers = {
    'User-Agent': 'sadasdsafdgdsd'
}
url = 'https://www.jdlingyu.com/tuji'
response = requests.get(url=url,headers=headers).text
select = parsel.Selector(response)
title_list = select.css('.post-info h2 a::text').getall()
link_list = select.css('.post-info h2 a::attr(href)').getall()
for title,link in zip(title_list,link_list):
    if not os.path.exists(f'img/{title}'):
        os.mkdir(f'img/{title}')
    response_1 = requests.get(link,headers=headers)
    html_data = response_1.text
    select = parsel.Selector(html_data)
    img_all = select.css('.entry-content img::attr(src)').getall()
    for img in img_all:
        img_data = requests.get(img,headers=headers).content
        img_name = img.split('/')[-1]
        with open(f'img/{img_name}',mode='wb') as f:
            f.write(img_data)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python爬虫可以用来自动爬取百度图片,实现批量下载图片的功能。通过使用Python编程语言和相关的库函数,我们可以编写爬虫脚本来完成这个任务。 Python爬虫领域非常常用,因为它有着丰富的库函数和强大的功能,特别是在人工智能和图像处理方面有着突出的表现。 在爬取百度图片时,我们需要注意到百度图片的翻页方式是瀑布流,通过滚动滑轮触发JS进行异步加载,而不是之前的点击下一页的方式。这给爬虫增加了一定的难度。但是通过之前的文章,我们可以找到一篇描述了爬取百度图片的方法,其中介绍的方式可能还是之前的点击下一页的方式。 因此,如果你对爬虫有兴趣,可以尝试使用Python编写一个爬取百度图片的脚本,通过学习和实践,你可以更深入地了解爬虫的知识和应用。同时,你也可以不定期关注爬虫实例和教程,以获得更多的学习和交流的机会。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python 爬虫(1)--爬取百度图片,你确定不来看一看](https://blog.csdn.net/weixin_46897073/article/details/111827736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值