爬虫图片采集(自动化)

先提供一下最终的代码(已封装)

复制代码
import os.path
import time
import subprocess
from playwright.sync_api import sync_playwright
import config

class DataCollectors():
    # 启动浏览器
    def lanuch_chrom(self):
        params = F"--remote-debugging-port={config.PORT}"
        cmd = f'"{config.BROWSER_PATH}" {params}'
        self.browser = subprocess.Popen(cmd)#赋值给属性,相当于创建了一个浏览器对象

    # 下载资源
    def download(self, response):
        # print("下载方法")
        # print(response.url)
        # 如果保存图片的目录不存在,则创建
        if not os.path.exists(config.IMAGE_STORE):
            os.mkdir(config.IMAGE_STORE)

        # 获取相应头中文件类型
        content_type = response.headers.get("content-type")

        # 注意,保存图片的时候,名称不要重复
        if content_type is not None and content_type == "image/jpeg":
            print(response.url)
        else:
            print("不是图片")


    # 解析数据
    def parse_data(self, page):
        print("解析数据")
        for page_no in range(1):
            page.wait_for_timeout(config.WAIT_TIME * 1000)

            # # 定位所有电影信息
            # li_list = page.locator('//*[@id="main"]/div[3]/ul/li').all()
            # # print(li_list)
            # for li in li_list:
            #     title = li.locator('xpath=./a/b').inner_text()
            #     print(title)

            # 定位 后页 按钮
            next_ele = page.locator('//*[@id="main"]/div[4]/a[text()="下一页"]')
            if next_ele.count() != 0:
                # 滚动到下一页按钮所在位置
                next_ele.scroll_into_view_if_needed()
                page.wait_for_load_state("networkidle")
                page.wait_for_timeout(config.WAIT_TIME*1000)
                # 存在下一页
                print("存在下一页")
                next_ele.click()
            else:
                print("不存在下一页了!!")

    def main(self):
        # 启动浏览器
        self.lanuch_chrom()

        time.sleep(2)#时间自己控制,可以移到config中进行自己调整

        # 连接浏览器
        with sync_playwright() as pw:
            browser = pw.chromium.connect_over_cdp(F"http://{config.CONNECT_IP}:{config.CONNECT_PORT}")
            context = browser.contexts[0]
            page = context.pages[0]

            # 监听资源,下载图片(自己判断是否要下载)
            page.on("response", self.download)

            # 访问目标网站
            page.goto(config.START_URL)

            # 解析数据
            self.parse_data(page)

            page.wait_for_timeout(5000)

        # 关闭程序
        self.browser.terminate()


if __name__ == '__main__':
    dc = DataCollectors()
    dc.main()

以上的代码配置的config(控制很简单)

复制代码
# 大写的一般表示常量,不改变的量
# 启动浏览器参数
BROWSER_PATH = r"C:\Users\six\AppData\Local\ms-playwright\chromium-1187\chrome-win\chrome.exe"
PORT = 7899

# 爬虫程序参数
# 连接浏览器参数
CONNECT_IP = "127.0.0.1"
CONNECT_PORT = 7899

# 网站的起始 url
START_URL = 'https://pic.netbian.com/3200x2000/index_13.html'
# 等待时间【单位秒】
WAIT_TIME = 1
# 保存图片的文件夹
IMAGE_STORE = "images"

# //*[@id="main"]/div[4]/a[11]
# //*[@id="main"]/div[4]/a[9](两个xpath是不一样的,不能通过这个来判断)
相关推荐
m0_663234011 天前
Libvio.link爬虫技术与反爬攻防解析
爬虫
深蓝海拓1 天前
PySide6从0开始学习的笔记(二十五) Qt窗口对象的生命周期和及时销毁
笔记·python·qt·学习·pyqt
u0109272711 天前
模板编译期排序算法
开发语言·c++·算法
datalover1 天前
CompletableFuture 使用示例
java·开发语言
Dfreedom.1 天前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
理人综艺好会1 天前
Web学习之用户认证
前端·学习
2301_790300961 天前
用Python读取和处理NASA公开API数据
jvm·数据库·python
●VON1 天前
React Native for OpenHarmony:项目目录结构与跨平台构建流程详解
javascript·学习·react native·react.js·架构·跨平台·von
m0_686041611 天前
C++中的适配器模式变体
开发语言·c++·算法
清风~徐~来1 天前
【视频点播系统】WebSocketpp 介绍及使用
开发语言