python爬取robomaster论坛数据,作为后端数据

一. 内容简介

python爬取robomaster论坛数据,作为后端数据

二. 软件环境

2.1vsCode

2.2Anaconda

version: conda 22.9.0

2.3代码

三.主要流程

3.1 接口分析

javascript 复制代码
# 接口分析
# 全部数据
# https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63   20
# 机械设计
# https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63&filter=typeid&typeid=11   20
# 嵌入式
# https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63&filter=typeid&typeid=12    20
# 视觉算法
# https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63&filter=typeid&typeid=13   9
# 其他
# https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63&filter=typeid&typeid=14
# 分页
# &filter=typeid&page=3

3.2 通过selenium爬取网页结构

大疆这个网站是直接在服务端渲染好的,只能从结构里面爬了,不能直接拿接口数据了,content是整个网页结构

python 复制代码
import urllib.request
from lxml import etree
import json
from selenium.webdriver.common.by import By
from selenium import webdriver
import random 
import time
import pyautogui
from datetime import datetime
import random
def seleniumRequest(url,chrome_path,waitTime): 
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        options.add_experimental_option('useAutomationExtension', False)
        # 谷歌浏览器exe位置
        options.binary_location = chrome_path
        # 是否要启动页面
        # options.add_argument("--headless")  # 启用无头模式
        # GPU加速有时候会出bug
        options.add_argument("--disable-gpu")  # 禁用GPU加速
        options.add_argument("--disable-blink-features=AutomationControlled")
        driver = webdriver.Chrome(options=options)
        driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
        # 启动要填写的地址,这就启动浏览器
        driver.get(url)
        # 这是关闭浏览器
        # 等待页面加载,可以根据实际情况调整等待时间
        driver.implicitly_wait(waitTime)

        # 获取完整页面结构
        full_page_content = driver.page_source

        # 关闭浏览器
        driver.quit()

        return full_page_content
# # 处理完整页面结构
# print(full_page_content)
url = "https://bbs.robomaster.com/forum.php?mod=forumdisplay&fid=63&page=2"
print(url)

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
waitTime = 8
# 获取网页结构
# 通过selenium调用浏览器访问
content = seleniumRequest(url,chrome_path,waitTime)
print(content)

3.2 从网页结构中爬出数据,存入json文件中

python 复制代码
import random
from datetime import datetime, timedelta

def generate_random_date(start_date, end_date):
    random_days = random.randint(0, (end_date - start_date).days)
    random_date = start_date + timedelta(days=random_days)
    return random_date.strftime("%Y-%m-%d")

start_date = datetime(2021, 1, 1)
end_date = datetime(2023, 12, 31)

# 给html变成tree用于xpath解析用
tree = etree.HTML(content)
# 改进的XPath表达式,选择你感兴趣的div元素
# 解析对应数据
contents = tree.xpath("//*[starts-with(@id, 'normalthread')]")


img = contents[0].xpath(".//*[@id='aaa']//img/@src")

baseurl = "	https://bbs.robomaster.com/"

lists=[]
id = 1
for index, url in enumerate(contents):
    imgurl = contents[index].xpath(".//*[@id='aaa']//img/@src") 
    imgurl = baseurl + imgurl[0]

    all = contents[index].xpath(".//p//a//text()")
    url = contents[index].xpath(".//p//a/@href")
    url = baseurl + url[1]
    # 题目
    title = all[1]
    end_index = title.find('】')
    title = all[1][end_index+1:]
    end_index= 0
    if title == None:
        continue

    # 作者
    name = all[2]
    if "作者" not in name:
        continue
    # 查看
    view = random.randint(1000, 50000)
    # 评论
    comment = all[3]
    if "回复" not in comment:
        continue
    # 时间
    time = generate_random_date(start_date, end_date)

    # print(index,url)
    item = {
        id: id,
        'imgurl': imgurl,
        'title':title,
        'name':name,
        'view':view,
        'comment':comment,
        'time':time,
        'type':1,
        'url': url
    }


    lists.append(item)
    id = id + 1
json_data = json.dumps(lists, indent=4)
 # 将JSON数据写入文件
with open("data.json", "w") as json_file:
    json_file.write(json_data)
print("JSON数据已保存到文件")

3.4 json存入数据库中

python 复制代码
import json
import mysql.connector

# 读取JSON文件
with open('data.json', 'r') as file:
    data = json.load(file)

# 连接到MySQL数据库
conn = mysql.connector.connect(
    host='localhost',
    port=3306,  # MySQL默认端口号
    user='root',
    password='1234qwer',
    database='competitionassistant'
)

cursor = conn.cursor()

# 创建表(如果不存在的话),并清空表数据

    # item = {
    #     id: id,
    #     'imgurl': imgurl,
    #     'title':title,
    #     'name':name,
    #     'view':view,
    #     'comment':comment,
    #     'time':time,
    #     'type':1
    # }


cursor.execute('''
CREATE TABLE IF NOT EXISTS form_list  (
    id INT AUTO_INCREMENT PRIMARY KEY,
    imgurl VARCHAR(128),
    title VARCHAR(128),
    name VARCHAR(64),
    view VARCHAR(16),
    comment VARCHAR(16),
    time VARCHAR(16),
    type INT,
    url VARCHAR(128)
)
''')
# 先清空一下表

cursor.execute('TRUNCATE TABLE form_list')
# 将数据插入数据库
for item in data:
    # print(item)
    # cursor.execute('''
    #     INSERT INTO index_img (img_url, prod_id, seq, status, create_time, update_time)
    #     VALUES (%s, %s, %s, %s, %s, %s)
    # ''', (item['img_url'], item['prod_id'], item['seq'], item['status'], item['create_time'], item['update_time']))
    
    sql_statement = f"""
    INSERT INTO form_list (imgurl, title, name, view, comment, time, type, url)
    VALUES ('{item['imgurl']}', '{item['title']}', '{item['name']}', '{item['view']}', '{item['comment']}', '{item['time']}', '{item['type']}', '{item['url']}')
    """

    print(sql_statement)
    cursor.execute(sql_statement)

    print()
# 提交更改并关闭连接
conn.commit()
conn.close()

3.4 结果

相关推荐
Learn-Python11 分钟前
MongoDB-only方法
python·sql
ghie909023 分钟前
基于MATLAB GUI的伏安法测电阻实现方案
开发语言·matlab·电阻
Gao_xu_sheng28 分钟前
Inno Setup(专业安装/更新 EXE)
开发语言
小途软件1 小时前
用于机器人电池电量预测的Sarsa强化学习混合集成方法
java·人工智能·pytorch·python·深度学习·语言模型
吴声子夜歌1 小时前
Java数据结构与算法——基本数学问题
java·开发语言·windows
扫地的小何尚2 小时前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
wanglei2007082 小时前
生产者消费者
开发语言·python
清水白石0082 小时前
《从零到进阶:Pydantic v1 与 v2 的核心差异与零成本校验实现原理》
数据库·python
leo__5202 小时前
基于菲涅耳衍射积分的空心高斯光束传输数值模拟(MATLAB实现)
开发语言·matlab
昵称已被吞噬~‘(*@﹏@*)’~2 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战