Python爬虫爬图片测试1

idea配置集成Python开发环境

安装Python
配置环境变量
创建python项目,创建py文件,运行代码。

1、IDEA安装Python插件

Idea->settings->plugins->python

2、安装python sdk

官网下载地址 https://www.python.org/downloads/
解压后安装包MD5校验 https://www.python.org/downloads/release/python-381/,查md5值, 文件校验工具,win10软件商店中的Hash Checker,判断md5值是否一致
这里以python-3.7.4-amd64(64-bit)为例

Setup->Customize installation(自定义)
勾选install launcher for all users(recommended)
勾选 Add Python 3.7 to PATH

Optional Features
勾选 Documentation
勾选 pip
勾选 tcl/tk and IDLE
勾选 Python test suite
勾选 py launcher
勾选 for all users(requires elevation)

Advanced Options
勾选Associate files with Python(requires the py launcher)
勾选Create shortcuts for installed applications
勾选Add Python to environment variables
勾选Precompile standard library

Setup was successful->close 安装完成。

3、配置python环境变量

在系统环境变量Path的末尾加上
D:\python
D:\python\Scripts
这两个路径,第二个路径是为了方便使用pip , python的软件包管理工具,类似于maven。

4、创建python项目

New project ->python->project SDK->add project SDK-> System Interpreter->D:\python\python.exe

Python爬虫爬图片测试1

测试网站 http://www.netbian.com/
F12 浏览器控制台查看元素

取多页数据

运行代码实例

import requests
from lxml import etree
def get_image():
    try:
        headers = {
            'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Mobile Safari/537.36'
        }
        #保存文件路径
        path = "C://Users/shuang/Desktop/image2022/"
        #获取响应数据
        response = requests.get(url,headers=headers)
        response_data = response.content.decode('gbk')
        #判断是否有响应
        response_code = response.status_code
        print(response_code)
        #保存数据
        #with open('wall.html','w',encoding='gbk')as f:
        #  f.write(response_data)
        #数据解析
        #1.将数据解析为HTML
        parse_data = etree.HTML(response_data)
        #2.将需要的内容以字段的形式赋值给item
        item_list = parse_data.xpath('//div/ul/li/a/img/@src')
        #用for循环遍历整个列表并保存
        for item in item_list:
            final_data = requests.get(item,headers=headers).content
            with open(path + item[-7:], 'wb')as f:
                f.write(final_data)
    #print(item)
    except:
        print('error')
def get_page():
    #取前10页
    urls = ["http://www.netbian.com/index_{}.htm".format(str(i)) for i in range(1,3)]
    #输出验证
    print(urls)
    return urls
if __name__ == '__main__':#主函数
    get_page()
    for url in get_page():
        get_image()

数了一下index_2.htm页面的图,发现爬下来的图少了一张。 将代码中的 //div/ul/li/a/img/@src
改成 //div/ul/li/div/a/img/@src 就能获取到缺失的那张图。

图片解析路径配置

项目运行过程中的报错信息:
1、import requests报错
错误提示:pip版本需要升级
处理办法:
(已配置Python相关环境变量)在cmd窗口执行命令升级pip版本:python -m pip install --upgrade pip
然后回到idea开发环境下,点击import requests处的红线,提示install requests,点击安装即可。import requests报错解决。

2、from lxml import etree报错

from lxml import html
etree = html.etree

然后idea提示install package lxml,再使用from lxml import etree就正常了

代码来源 :https://blog.csdn.net/qq_41821603/article/details/109201092
掌握参数替换和依赖库引入的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅子专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值