首页 > 其他分享 >动态加载页面的爬虫方法

动态加载页面的爬虫方法

时间:2023-07-18 18:11:20浏览次数:32  
标签:count get text driver 爬虫 source print 加载 页面

首先,可以直接手动拉到网页最下面,然后把F12里面的网页节点元素复制成文本,去获取目标进行下载,代码如下,用到的库BeautifulSoup:

import os
import urllib.request
import re
from bs4 import BeautifulSoup as bs
import random as rd
import time
def get_imgs(text):
    soup = bs(text, features="lxml")
    imgss = soup.find_all("img")
    print(imgss)
    pattern = re.compile('src=\"([^\"]+)')
    source = pattern.findall(str(imgss))
    print("===========")
    for pic in source:
        print(pic)
    print("===========")
    print("本页一共有" + str(len(source)) + "页漫画")
    count = 0

    for item in source:
        count = count + 1
        if count < 11:
            continue
        print('第' + str(count) + '个')
        p = str(count) if count > 9 else "0"+str(count)
        name = "c05-p"+p
        download(item, "manga-白圣女与黑牧师", "c06", name)
        time.sleep(rd.randint(2, 4))

    print("---------爬取结束--------")
    return len(source)

def download(realUrl, dir, subDir, name):
    path = subDir + '/'
    if not os.path.exists(path):
        os.makedirs(path)
    try:
        urllib.request.urlretrieve(realUrl, '{0}/{1}.jpg'.format(path, name))
    except Exception as e:
        print("发生运行时异常:", e)
    finally:
        pass

text = """
<div id="images"><img src="https://n1a.zhjyu.net/images/p/c9/6b/888e04ccb13ae1d2d075142f4fe4.jpg" data-index="1" style="display: 
略...
/190aaf19e561363d805e437485da.jpg" data-index="40" style="display: inline;"><p class="img_info">(40/40)</p></div>
"""

get_imgs(text)

自动化的方法就要模拟浏览器网页下拉的操作了,用到库selenuim。

第一步要做的就是,用selenium打开浏览器然后打开指定网页。

def get_dynamic_text_through_webdriver():
    driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
    driver.get("https://www.guoguomh.com/manhua/baishengnvyuheimushi/496885.html")  # 第七话

报错如下:

Traceback (most recent call last):
  File "guoguomh.py", line 65, in <module>
    get_dynamic_text_through_webdriver()
  File "guoguomh.py", line 15, in get_dynamic_text_through_webdriver
    driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
......略
selenium.common.exceptions.WebDriverException: Message: Service C:/Program Files (x86)/Google/Chrome/Application/chrome.exe unexpectedly exited. Status code was: 0

代码修改如下:

def get_dynamic_text_through_webdriver():
    driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
    driver.get("https://www.guoguomh.com/manhua/baishengnvyuheimushi/496885.html")  # 第七话

仍然报错:

......略
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 92 Current browser version is 114.0.5735.199 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

 可能是chromedriver的版本问题,按照报错信息里面给定的Browser Version版本,在网站“https://chromedriver.chromium.org/downloads”里下载对应版本的driver(114版本就可)。然后解压,把chromedriver.exe放到Chromer浏览器chrome.exe同一目录下即可。

启动成功。

接下来就是爬虫的经典流程。

遇到问题如下

def get_dynamic_text_through_webdriver():
    driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
    try:
        driver.get("https://www.guoguomh.com/manhua/baishengnvyuheimushi/496885.html")  # 第七话
    except Exception as e:
        print(e)
    driver.maximize_window()
    driver.execute_script("document.getElementById('images').scrollTo=12000")
    time.sleep(6)  # 定时6s等待内容加载
    print("定时结束,爬取内容")
    html_source = driver.page_source
    print(html_source)

由此获取到的页面源码不标准,因此不应使用driver.page_source获取页面文档,而应是执行JavaScript代码返回页面文档或者直接调用driver的xpath功能。

完整代码如下(尚有缺陷,个别图片下载损坏,将用try-catch来跳过,避免程序中断):

# -*- coding: utf-8 -*-
# @Author : Zhao Ke
# @Time : 2023-07-18 11:58
import os
import re
import time
import random as rd
import urllib.request
from bs4 import BeautifulSoup as bs
from selenium import webdriver


def get_dynamic_text_through_webdriver(page_url):
    driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
    try:
        driver.get(page_url)  # 第七话
    except Exception as e:
        print(e)
    driver.maximize_window()
    driver.execute_script("document.getElementById('images').scrollTo=12000")
    time.sleep(2)  # 定时等待加载
    print("定时结束,爬取内容")

    # 初值
    html_source = driver.find_element_by_xpath('//*[@id="images"]')
    indices = html_source.text[-7:]  # 获取指定元素里的文档里面的文本
    curr = int(indices[1:3])
    maxi = int(indices[4:6])
    # 迭代直到下拉到最后
    while curr < maxi:
        driver.execute_script("window.scrollBy(0, 10000)")
        html_source = driver.find_element_by_xpath('//*[@id="images"]')
        indices = html_source.text[-7:]  # 获取指定元素里的文档里面的文本
        curr = int(indices[1:3])
        maxi = int(indices[4:6])
        time.sleep(0.5)
    return html_source.get_attribute("outerHTML")  # 获取指定元素的文档


def get_imgs(text, chapter):
    soup = bs(text, features="lxml")
    imgss = soup.find_all("img")
    print(imgss)
    pattern = re.compile('src=\"([^\"]+)')
    source = pattern.findall(str(imgss))
    print("===========")
    for pic in source:
        print(pic)
    print("===========")
    print("本页一共有" + str(len(source)) + "页漫画")
    count = 0

    for item in source:
        count = count + 1
        if count < 1:
            continue
        print('第' + str(count) + '个')
        p = str(count) if count > 9 else "0"+str(count)
        name = "c"+chapter+"-p"+p
        download(item, "baishengnvyuheimushi", "c"+chapter, name)
        time.sleep(rd.randint(1, 3))
    print("---------爬取结束--------")
    return len(source)


def download(realUrl, dir, subDir, name):
    path = "D:/kingz/ANIME/"+dir+'/'+subDir
    if not os.path.exists(path):
        os.makedirs(path)
    try:
        urllib.request.urlretrieve(realUrl, '{0}/{1}.jpg'.format(path, name))
    except Exception as e:
        print("发生运行时异常:", e)
        print("跳过该页", name)
    finally:
        pass


chapter = "07"  # 章节名
url = "https://www.guoguomh.com/manhua/baishengnvyuheimushi/496885.html"  # 爬取页面
text = get_dynamic_text_through_webdriver(url)
get_imgs(text, chapter)

改进空间:把网页拉到底才开始爬虫,影响效率,可以尝试采用多线程同时进行,或者是否可以采用异步框架;研究有一些图片为什么会损坏,是否是网络的原因。

end.

标签:count,get,text,driver,爬虫,source,print,加载,页面
From: https://www.cnblogs.com/zhaoke271828/p/17562622.html

相关文章

  • JVM类的加载和加载器
    JVM类的加载和类的加载器一.类的加载过程类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构。类的加载的最终产品是位于堆区中的Class对象,Class对象封装了类在方法......
  • 前端的一个问题——前端页面打不开
    打开一个webapp下的页面会报这个错误原因:因为第三个函数拦截了所有的路径,所以web页面显示不出来解决方法:在创建一个放行的配置文件,把webapp中的四个目录放行,就可以了......
  • python监测页面元素事件
    Python监测页面元素事件实现教程1.简介在进行Web开发中,我们常常需要对页面元素的状态进行监测,例如监听用户的点击、鼠标移动等事件。本教程将教会你如何使用Python来实现对页面元素的事件监测。2.流程概述下面是实现页面元素事件监测的整个流程概述:步骤描述1导入......
  • python加载多光谱照片
    Python加载多光谱照片在现实生活中,我们经常使用彩色照片来记录和分享我们的回忆。然而,有时候我们需要更多的信息来了解照片中的物体和场景。这就需要使用到多光谱照片,它们可以提供不同波长范围的图像信息。多光谱照片通常由一些特殊的相机或传感器捕获,这些设备能够同时记录不同波......
  • python加载npz
    Python加载npz文件在Python中,我们经常需要加载和保存数据。而NumPy是Python中一个非常常用的科学计算库,其中提供了一个功能强大的数据存储格式——npz。npz是NumPy自定义的压缩格式,它可以用于存储多个数组,并且可以通过键值对的方式访问数组。这使得npz成为在科学计算和机器学习中......
  • 类加载的过程是什么?
    转载:https://www.bilibili.com/video/BV1RV4y117an/?spm_id_from=333.337.search-card.all.click&vd_source=46d50b5d646b50dcb2a208d3946b1598......
  • django查询-列延迟加载only()、defer()
    这玩意和sqlalchemy的几乎一样。only():只加载给定的列,其他列只有在使用时会发起二次查询defer():不加载指定的列,刚好和only()相反。实例:>>>ret=BookInfo.objects.get(id=1).only("name")#1、先导入connection,获取django查询的所有sql语句>>>fromdjango.dbimportconn......
  • hive页面介绍
    Hive页面介绍Hive是一个基于Hadoop的数据仓库工具,它提供了一个简单的查询语言——HiveQL,用于分析和查询大规模数据集。Hive将SQL查询转换为MapReduce任务,在Hadoop集群上执行,因此可以处理大量的数据。Hive页面的作用Hive页面是Hive的用户界面,提供了一个交互式的方式来执行查询和......
  • ios 加载网络图片
    iOS加载网络图片在iOS开发中,我们经常需要从网络上加载图片并显示到用户界面上。本文将介绍一种简单的方法来加载网络图片,并附带代码示例。使用第三方库SDWebImageSDWebImage是一个广泛使用的第三方库,它提供了一种简单而高效的方式来加载网络图片,并支持图片缓存和缓存管理。......
  • Java爬虫--HttpClient-Post请求
    //下面是一个demo:packagetest;importorg.apache.http.HttpEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util......