当前位置: 首页 > news >正文

淄博企业网站建设有限公司长沙关键词优化费用

淄博企业网站建设有限公司,长沙关键词优化费用,深圳市网站备案,上海公司排名大全写在前面 工作中遇到,简单整理理解不足小伙伴帮忙指正 对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大…

写在前面


  • 工作中遇到,简单整理
  • 理解不足小伙伴帮忙指正

对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大众理想的懦弱回归,是随波逐流,是对内心的恐惧 ——赫尔曼·黑塞《德米安》


数据采集的过程中,有部分页面会在接口调用到一定次数之后,每次获取数据调用接口之后,弹出一个验证码的校验,作为一种反爬措施,对于这种接口调用验证码,一般情况下,是只要请求就跳转,有部分页面是随机的,比如页面中有好多搜索框,可能每个搜索框的change 事件都会发生一次接口调用,这个时候使用 selenium 自动化提提取数据,会导致处理的页面不是想要的的页面,所以对于这种验证码的处理,我们需要在页面任意位置,提供一个检测跳转验证码验证页面的方法,同时对验证码做校验处理。

下面为一个 Demo

def cap(driver):"""@Time    :   2023/08/29 03:38:33@Author  :   liruilonger@gmail.com@Version :   1.0@Desc    :   验证码处理Args:driverReturns:void"""import ddddocrocr = ddddocr.DdddOcr()time.sleep(3)while  len(driver.find_elements(By.XPATH,"//h1[contains(text(),'输入验证码刷新') ] " )) > 0:element = driver.find_element(By.XPATH, "//img[ @id='vcodeimg' ]")# 清空验证码数据driver.execute_script("arguments[0].value = ''", element)#定位元素并获取截图文件: element.screenshot("element.png")with open("element.png", "rb") as f:image_bytes = f.read()#image_bytes = BytesIO(base64.b64decode(screenshot))text = ocr.classification(image_bytes)if len(text) >4:text = text[1:5]driver.find_element(By.XPATH, "//input[@id='vcode']").send_keys(text)time.sleep(3)driver.find_element(By.XPATH, "//input[@class='isOK']").click()time.sleep(3)# 验证失败重新验证if  len(driver.find_elements(By.XPATH,"//h1[contains(text(),'输入验证码刷新') ] " )) > 0:driver.get("https://icp.chinaz.com/captcha")

在实际的编写中需要注意的地方:

  • 获取验证码图片的方式,是通过对元素截图,还是对照片路径请求下载获取,需要注意有些验证码图片,在通过 requests 库下载图片时,每次调用都是不同的图片,所以只能使用截图的方式
  • 验证码识别的方式,可以考虑使用 ocr或者深度学习模型,或者一些商业接口,上面使用的 pip install ddddocr
  • 对于识别不准的情况,可以考虑做一些后期的约束处理,比如上面的验证码,4位数字,但是在第一位会出现一个干扰字符,ocr 偶尔会识别为字符,需要做切割处理。
  • 进行识别的时机,以及识别后的处理,对于如何开始识别,可以通过关键字来进行判断,放到入口处,对于识别后验证失败的处理也需要考虑,上面的页面在识别验证成功会进行跳转,错了不发生跳转
  • 对于错误的情况,可以使用死循环的,重新请求,获取新的验证码,直到识别验证成功。

下面为一个数据采集的实际脚本中的使用。

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   icp_reptile.py
@Time    :   2023/08/23 23:07:46
@Author  :   Li Ruilong
@Version :   1.0
@Contact :   liruilonger@gmail.com
@Desc    :   验证码版本
"""# here put the import libfrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import re
import pandas as pd
import csv
import sys
import os 
import json
import requests
import ddddocr
from io import BytesIO
import base64
import pytesseract
from PIL import Imagea_name = ['河北']
ocr = ddddocr.DdddOcr()"""
自动登陆,需要提前保存登陆cookie 信息
"""
driver = webdriver.Chrome()
with open('C:\\Users\山河已无恙\\Documents\GitHub\\reptile_demo\\demo\\cookie_vip.json', 'r', encoding='u8') as f:cookies = json.load(f)driver.get('https://icp.chinaz.com/provinces')
for cookie in cookies:driver.add_cookie(cookie)driver.get('https://icp.chinaz.com/provinces')wait = WebDriverWait(driver, 30)## 查询条件准备"""
查询条件准备
"""#wait.until(EC.presence_of_element_located((By.XPATH, "//span[ @title='chinaz_7052291' ]")))def cap(driver):"""@Time    :   2023/08/29 03:38:33@Author  :   liruilonger@gmail.com@Version :   1.0@Desc    :   验证码处理Args:Returns:void"""time.sleep(3)while  len(driver.find_elements(By.XPATH,"//h1[contains(text(),'输入验证码刷新') ] " )) > 0:element = driver.find_element(By.XPATH, "//img[ @id='vcodeimg' ]")# 清空验证码数据driver.execute_script("arguments[0].value = ''", element)#定位元素并获取截图文件: element.screenshot("element.png")with open("element.png", "rb") as f:image_bytes = f.read()#image_bytes = BytesIO(base64.b64decode(screenshot))text = ocr.classification(image_bytes)if len(text) >4:text = text[1:5]driver.find_element(By.XPATH, "//input[@id='vcode']").send_keys(text)time.sleep(3)driver.find_element(By.XPATH, "//input[@class='isOK']").click()time.sleep(3)if  len(driver.find_elements(By.XPATH,"//h1[contains(text(),'输入验证码刷新') ] " )) > 0:driver.get("https://icp.chinaz.com/captcha")time.sleep(5)
# 触发验证码处理
all_butt_cap =  driver.find_element(By.XPATH,"//h1[contains(text(),'输入验证码刷新') ] " )# 处理验证码的情况cap(driver)time.sleep(5)### 查询条件准备# 备案时间
all_butt =  driver.find_element(By.XPATH,"//div/a[contains(@href,'all') and @class='pr10' ] " )
driver.execute_script("arguments[0].click();", all_butt)
cap(driver)
# 单位性质 
all_butt =  driver.find_element(By.XPATH,"//div[contains(text(),'全部') and @class='MainCateW-cont SearChoese'] " )
driver.execute_script("arguments[0].click();", all_butt)
time.sleep(3)
cap(driver)
# 企业
all_butt =  driver.find_element(By.XPATH,"//a[contains(text(),'企业') and @val='企业' ]" )
driver.execute_script("arguments[0].click();", all_butt)
time.sleep(2)cap(driver)
# 状态
all_butt =  driver.find_element(By.XPATH,"//div[contains(text(),'全部') and @id='webStatus_txt'  and @class='MainCateW-cont SearChoese w90'] " )driver.execute_script("arguments[0].click();", all_butt)
time.sleep(2)
# 已开通
all_butt =  driver.find_element(By.XPATH,"//a[contains(text(),'已开通') and @val='1'  ]" )driver.execute_script("arguments[0].click();", all_butt)
time.sleep(2)
cap(driver)# 地区
all_butt =  driver.find_element(By.XPATH,"//strong[contains(text(),'地区:') and @class='CateTit'  ]" )
next_element = all_butt.find_element(By.XPATH,"following-sibling::*[1]")
driver.execute_script("arguments[0].click();", next_element)
time.sleep(2)def area(p_name,driver,p_data):"""@Time    :   2023/08/24 04:24:50@Author  :   liruilonger@gmail.com@Version :   1.0@Desc    :   备案数据获取Args:Returns:void"""all_butt =  driver.find_element(By.XPATH,"//a[contains(text(),'"+p_name+"')   ]" )driver.execute_script("arguments[0].click();", all_butt)#all_butt.click()time.sleep(2)# 页数太对分盟市处理,后面的数据没办法直接处理all_butt =  driver.find_element(By.XPATH,"//div[contains(text(),'全部')  and @id='addrctxt'  ]" )time.sleep(2)driver.execute_script("arguments[0].click();", all_butt)all_ui =  driver.find_element(By.XPATH,"//ul[ @id='addrclst'] " )citys =  all_ui.find_elements(By.TAG_NAME,'a')for city in  citys:try:c_n =city.textexcept:print("页面异常")continueif c_n == '全部':continueprint("处理市:",c_n)time.sleep(2)driver.execute_script("arguments[0].click();", city)time.sleep(5)# 验证码处理cap(driver)time.sleep(5)# 查询cap(driver)# 选择全部all_butt =  driver.find_element(By.XPATH,"//div/a[contains(@href,'all') and @val='all'  ] " )driver.execute_script("arguments[0].click();", all_butt)time.sleep(5)cap(driver)#all_butt =  driver.find_element(By.XPATH,"//input[ @type='button' and @id='btn_search'  and @value='点击搜索'] " )#driver.execute_script("arguments[0].click();", all_butt)#time.sleep(4)#cap(driver)time.sleep(10)def all_break(driver):if len(driver.find_elements(By.XPATH,"//span[contains(text(),'页,到第')  and @class='col-gray02'] " )) == 0:all_butt =  driver.find_element(By.XPATH,"//div/a[contains(@href,'all') and @val='all'  ] " )driver.execute_script("arguments[0].click();", all_butt)time.sleep(5)cap(driver)time.sleep(10)#all_break(driver)# 总页数获取page_butt =  driver.find_element(By.XPATH,"//span[contains(text(),'页,到第')  and @class='col-gray02'] " )page_c  = int(re.search(r'\d+', page_butt.text).group())# 盟市页数太多分时间段处理 # 当前页数据处理tbody =  driver.find_element(By.XPATH,"//tbody[ @class='result_table' and @id='result_table' ]")rows = tbody.find_elements(By.TAG_NAME,'tr')for row in rows:# 获取当前行中的所有单元格cells = row.find_elements(By.TAG_NAME, "td")# 打印单元格数据data = {}data['域名']=cells[0].textdata['主办单位名称']=cells[1].textdata['网站首页网址']=cells[5].textp_data.append(data)# 其他页数据处理 if page_c >= 101:page_c = 101 for page_i in range(1,page_c):try:print(f"{c_n} :处理页数",page_i)nextPage =  driver.find_element(By.XPATH,"//a[ @title='下一页' and @id='nextPage' ]")driver.execute_script("arguments[0].click();", nextPage)time.sleep(3)cap(driver)all_break(driver)cap(driver)#nextPage.click()time.sleep(6)tbody =  driver.find_element(By.XPATH,"//tbody[ @class='result_table' and @id='result_table' ]")rows = tbody.find_elements(By.TAG_NAME,'tr')print(tbody.text)for row in rows:# 获取当前行中的所有单元格cells = row.find_elements(By.TAG_NAME, "td")# 打印单元格数据data = {}data['域名']=cells[0].textdata['主办单位名称']=cells[1].textdata['网站首页网址']=cells[5].textp_data.append(data)time.sleep(6)except:print(f"第 { page_i} 页发生了异常,跳过了")pass finally: fieldnames = ['域名', '主办单位名称', '网站首页网址']with open('省份_'+a+'_'+"ICP"+'.csv', 'w', newline='',encoding='utf-8') as file:writer = csv.DictWriter(file, fieldnames=fieldnames)writer.writeheader()  # 写入列名writer.writerows(p_data)  # 写入字典数据print("数据已保存为CSV文件",'  CDN_M_省份_'+a+'_'+'ICP'+'.csv')        return p_data        if __name__ == '__main__':for  a in a_name:p_data= []try:p_data = area(a,driver,p_data)except:continuepassfinally:fieldnames = ['域名', '主办单位名称', '网站首页网址']with open('省份_'+a+'_'+"ICP"+'.csv', 'w', newline='',encoding='utf-8') as file:writer = csv.DictWriter(file, fieldnames=fieldnames)writer.writeheader()  # 写入列名writer.writerows(p_data)  # 写入字典数据print("数据已保存为CSV文件",'  CDN_M_省份_'+a+'_'+'ICP'+'.csv')  time.sleep(55555)

博文部分内容参考

© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知 😃



© 2018-2023 liruilonger@gmail.com, All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)

http://www.ds6.com.cn/news/75002.html

相关文章:

  • wordpress 500 - 内部服务器错误长沙百度快照优化排名
  • 郑州网站建设公司排名济南seo网站关键词排名
  • 网站导航栏全屏怎么做简单网页制作
  • 个人摄影网站源码网站统计工具有哪些
  • 温州建站费用二十条优化疫情措施
  • 做招聘网站都需要什么手续360搜索引擎入口
  • dw个人网站制作模板全网网站快速排名推广软件
  • jsp servlet 网站实例seo关键词优化
  • 少女映画wordpress在哪里可以免费自学seo课程
  • 怎么接网站来做软文台
  • 怎么做网站背景图片在线网站建设平台
  • 编程软件做网站的宁夏百度公司
  • 福州 哈尔滨网站建设 网络服务许昌seo推广
  • wordpress首页添加视频西安seo服务商
  • wordpress主题加授权方式长沙seo步骤
  • 做app找哪个网站媒体发稿网
  • 旅游网站建设方案简介semantic scholar
  • 泰安网络推广公司平台廊坊seo排名霸屏
  • 外贸网站怎么做效果好打开搜索引擎
  • 阿里云编辑建设好的网站seo网站优化怎么做
  • 做公司网站建设价格低整站seo优化公司
  • 个人主页写什么内容上海牛巨微seo关键词优化
  • 哈尔滨网站建设王道下拉強关键词完整版免费听
  • ftp怎么找网站后台如何做好平台推广
  • 做设计太依赖网站素材360优化大师旧版
  • 怎么识别网站是用什么语言做的网络运营团队
  • 网站开发费属于无形资产那部分营销型网站
  • 村级网站建设 不断增强百家号权重查询站长工具
  • 高端网站建设公司费用制作网站模板
  • 郑州网站建设老牌公司无线新闻台直播app下载