python 写自动点击爬取数据

今天来点不一样的!哥们

提示: 这里只是用于自己学习的 ,请勿用违法地方

效果图

会进行点击下一页 进行抓取

需要其他操作也可以自己写


文章目录


前言

爬虫是指通过编程自动化地获取互联网上的信息的过程。在Python中,有许多强大的库和框架可用于实现爬虫,其中最常用的是Beautiful Soup和Requests库。

在开始编写爬虫之前,有一些重要的考虑事项:

合法性和道德性: 确保你的爬虫活动是合法的,并遵守网站的使用规定。爬虫不应该违反任何法律或侵犯隐私权。

robots.txt 文件: 在爬取网站之前,检查网站的robots.txt文件,这是网站所有者用来指导爬虫的文件。尊重这些规则以避免潜在的法律问题。

频率和速率: 控制爬虫的访问速率,以防止对服务器造成过大的负担。设置适当的延迟和间隔,以模拟真实用户的行为。

HTML基础: 了解基本的HTML结构和标签,因为大多数爬虫任务都涉及到解析HTML文档。


提示:以下是本篇文章正文内容,下面案例可供参考

一、上代码?

python 复制代码
import time

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

url = "https://ref.cnki.net/REF/AdvSearch/Index?colName=%E8%A2%AB%E5%BC%95%E4%B8%BB%E9%A2%98&colValue=%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6&isJump=true#toolbarDiv"

# 使用Selenium进行动态加载
driver = webdriver.Chrome()
driver.get(url)


def scrape_page():
    # 等待一些时间,确保页面加载完成
    time.sleep(5)

    # 获取当前页面高度
    page_height = driver.execute_script(
        "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")

    # 设置滚动步长
    scroll_step = 500

    # 模拟滚动
    for i in range(0, page_height, scroll_step):
        driver.execute_script("window.scrollTo(0, {});".format(i))
        time.sleep(1)  # 等待一些时间,确保内容加载

    # 获取滚动后的页面源代码
    html = driver.page_source

    # 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(html, 'html.parser')

    # 查找id为listContWrapper的div元素
    list_cont_wrapper = soup.find('div', {'id': 'listContWrapper'})

    # 如果找到了listContWrapper元素
    if list_cont_wrapper:
        # 在listContWrapper内部查找listCont和table
        list_cont = list_cont_wrapper.find('div', {'class': 'listCont'})
        table_elements = list_cont_wrapper.find_all('table')

        # 处理listCont的内容,根据实际情况进行调整选择器
        if list_cont:
            list_cont_data = list_cont.get_text(strip=True)
            print("listCont数据:", list_cont_data)

        # 遍历每个表格元素
        for table_element in table_elements:
            # 提取表格中的数据,可以根据实际情况进一步调整选择器
            rows = table_element.find_all('tr')
            for row in rows:
                # 提取每行中的单元格数据
                cells = row.find_all(['td', 'th'])
                row_data = [cell.get_text(strip=True) for cell in cells]
                print("表格行数据:", row_data)
    else:
        print("未找到id为listContWrapper的div元素")


# 初始抓取
scrape_page()

# 循环点击下一页按钮,直到没有下一页为止
while True:
    try:
        # 找到下一页按钮并点击
        next_page_button = driver.find_element(By.XPATH, '//a[@class="next"]')
        next_page_button.click()

        # 继续抓取下一页
        scrape_page()
    except NoSuchElementException:
        print("没有找到下一页按钮,退出循环。")
        break
# 关闭驱动
driver.quit()

总结

我这个是抓取然后进行点击然后又进行抓取,还挺好玩,

要弄其他的都是这种格式模板

啊哈~~~

相关推荐
Volunteer Technology5 小时前
携程智能体项目
人工智能·python·numpy
平安的平安6 小时前
Python实现RAG检索增强生成:让大模型拥有你的私有知识库
开发语言·python
code bean6 小时前
【LangChain】少样本提示(Few-Shot Prompting)实战指南
开发语言·python·langchain
心.c6 小时前
RAG文档解析 - pypdf、LlamaParse、DeepDoc、SimpleDirectoryReader到底怎么选?
python·算法·ai
㳺三才人子6 小时前
初探 OpenCV 圖像處理
人工智能·python·opencv·计算机视觉
财经资讯数据_灵砚智能6 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月10日
人工智能·python·信息可视化·自然语言处理·ai编程
金玉满堂@bj6 小时前
# Groovy Pipeline 详解
pip
hmywillstronger6 小时前
【Python】从SAP2000 XML截面库提取数据到Excel
xml·python·excel
常常有6 小时前
中间件与依赖系统:构建高效 Web 后端的双重利器
开发语言·python·中间件·fastapi
Zephyr_06 小时前
python基础
python