6-10日报

对于民法典的解析首先要做的是对数据的爬取,因此挑选好网站后即可,对网站结构进行分析后即可对数据爬取。观察到网站的数据属于动态加载的,因此在爬取的时候选择使用过selenium进行爬取,实现使用selenium模拟人的行为实现点击,加载数据后进行爬取,然后将爬取到的信息存入到MySQL中以便后续机器学习模型训练,爬虫代码如下:

import time
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import pymysql
db = pymysql.connect(host="localhost", user="root", passwd="", database="pypachong")
cursor = db.cursor()
web = webdriver.Edge()
web.get('https://anli.court.gov.cn/static/web/index.html#/alk/list')

time.sleep(1)
minbtn = web.find_element_by_xpath('//*[@id="alk"]/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/span[1]')
ActionChains(web).move_to_element(minbtn).click().perform()
time.sleep(1)
lbdivs = web.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[1]/div[2]/div[7]/div[1]')
ActionChains(web).move_to_element(lbdivs).click().perform()
time.sleep(2)
ul_list = web.find_elements_by_xpath('//*[@id="alk"]/div[2]/div/div[2]/div[2]/ul/li')

for num in range(3):
time.sleep(3)
for li in ul_list:
mtext = li.find_element_by_xpath('./div[2]/div[3]/div/div[2]/span[2]').text.replace(',','')
va = []
va.append(mtext)
va.append("知识产权法")
sql1 = "insert into yaodlaw (yaodian,type) values(%s,%s) "
cursor.execute(sql1, va)
db.commit()
time.sleep(0.5)
#print(li.find_element_by_xpath('./div[2]/div[3]/div/div[2]/span[2]').text.replace(',',''))
web.find_element_by_xpath('//*[@id="alk"]/div[2]/div/div[2]/div[3]/div/button[2]').click()
根据不同的类别分别爬取后存入数据库以便后续分析。
posted @ 2022-06-10 21:21  软工新人  阅读(29)  评论(0编辑  收藏  举报