爬虫学习1
ztj100 2025-05-22 14:57 8 浏览 0 评论
最近学习了下爬虫,测试可以通过,先留着
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import urllib.request
import json
import os
import cv2
from webdriver_manager.chrome import ChromeDriverManager
def login_and_get_uid():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# try:
# pass
# except Exception as e:
# print("eee:", e)
# finally:
# driver.quit()
driver.get("https://yyy.xxxxx.com/login")
print(driver.title)
driver.implicitly_wait(3)
print(driver.title)
# print("User Agent:", user_agent)
driver.find_element(By.NAME, 'username').send_keys("yyyyy")
driver.find_element(By.NAME, 'password').send_keys("xxxx")
# 找到登录按钮并点击
login_button = driver.find_element(By.CLASS_NAME, 'btn-submit')
login_button.click()
time.sleep(1)
img_element = driver.find_element(By.XPATH, '//img[@class="validate_big"]')
src_value = img_element.get_attribute("src")
print(src_value)
img_element_2 = driver.find_element(By.XPATH, '//img[@class="validate_block"]')
src_value_2 = img_element_2.get_attribute("src")
print(src_value_2)
if os.path.exists('./src_value.png'):
os.remove('./src_value.png')
urllib.request.urlretrieve(src_value, './src_value.png')
if os.path.exists('./src_value_2.png'):
os.remove('./src_value_2.png')
urllib.request.urlretrieve(src_value_2, './src_value_2.png')
smallImage = driver.find_element(By.XPATH, '//*[@id="slide"]/div/div[2]/div[2]')
newDis = template_matching('./src_value.png', './src_value_2.png')
print(newDis)
driver.implicitly_wait(5)
ActionChains(driver).click_and_hold(smallImage).perform()
ActionChains(driver).move_by_offset(xoffset=newDis, yoffset=0).perform()
ActionChains(driver).release().perform()
time.sleep(2)
print("ab:")
time.sleep(2)
driver.get("https:/www.sohuc.om")
time.sleep(3)
# xAuth = driver.execute_script("return window.sessionStorage.getItem('x-auth');")
# access = driver.execute_script("return window.sessionStorage.getItem('access');")
#
max_ane = {
"token": driver.execute_script("return window.sessionStorage.getItem('x-auth');"),
"user_agent": driver.execute_script("return navigator.userAgent;"),
"site_info": json.loads(driver.execute_script("return window.sessionStorage.getItem('access');")),
}
return {"max_ane": max_ane, "fin_ane": None}
def template_matching(img_path, tm_path):
# 导入图片,灰度化
img_rgb = cv2.imread(img_path)
template_rgb = cv2.imread(tm_path)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
tm_gray = cv2.cvtColor(template_rgb, cv2.COLOR_BGR2GRAY)
# 缺口图去除背景
h, w = tm_gray.shape
w_start_index, h_start_index = 0, 0
w_end_index, h_end_index = w, h
# 缺口图去除背景
# 算出高起始位置
for i in range(h):
if not any(tm_gray[i, :]):
h_start_index = i
else:
break
# 算出高的结束位置
for i in range(h - 1, 0, -1):
if not any(tm_gray[i, :]):
h_end_index = i
else:
break
# 算出宽的起始位置
for i in range(w):
if not any(tm_gray[:, i]):
w_start_index = i
else:
break
# 算出宽的起始位置
for i in range(w - 1, 0, -1):
if not any(tm_gray[:, i]):
w_end_index = i
else:
break
# 取出完整的缺口图
tm_gray = tm_gray[h_start_index:h_end_index + 1, w_start_index:w_end_index + 1]
# 自适应阈值话
img_thresh = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 0)
tm_thresh = cv2.adaptiveThreshold(tm_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 0)
# 边缘检测
img_canny = cv2.Canny(img_thresh, 0, 500)
tm_canny = cv2.Canny(tm_thresh, 0, 500)
# cv2.imshow("img_canny", img_canny)
# cv2.imshow("tm_canny", tm_canny)
h, w = tm_gray.shape[:2]
# 模板匹配
res = cv2.matchTemplate(img_canny, tm_canny, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
left_up = max_loc
print(left_up)
right_bottom = (max_loc[0] + w, max_loc[1] + h) # 右下角
# 圈出矩形坐标
cv2.rectangle(img_rgb, max_loc, right_bottom, (0, 0, 255), 2)
if os.path.exists('res.png'):
os.remove('res.png')
cv2.imwrite('res.png', img_rgb)
# 显示图片 参数:(窗口标识字符串,imread读入的图像)
# cv2.imshow("test_image", img_rgb)
return max_loc[0] / 400 * 238
if __name__ == '__main__':
re = login_and_get_uid()
print("re : ", re)
相关推荐
- 拒绝躺平,如何使用AOP的环绕通知实现分布式锁
-
如何在分布式环境下,像用synchronized关键字那样使用分布式锁。比如开发一个注解,叫@DistributionLock,作用于一个方法函数上,每次调方法前加锁,调完之后自动释放锁。可以利用Sp...
- 「解锁新姿势」 兄dei,你代码需要优化了
-
前言在我们平常开发过程中,由于项目时间紧张,代码可以用就好,往往会忽视代码的质量问题。甚至有些复制粘贴过来,不加以整理规范。往往导致项目后期难以维护,更别说后续接手项目的人。所以啊,我们要编写出优雅的...
- 消息队列核心面试点讲解(消息队列面试题)
-
Rocketmq消息不丢失一、前言RocketMQ可以理解成一个特殊的存储系统,这个存储系统特殊之处数据是一般只会被使用一次,这种情况下,如何保证这个被消费一次的消息不丢失是非常重要的。本文将分析Ro...
- 秒杀系统—4.第二版升级优化的技术文档二
-
大纲7.秒杀系统的秒杀活动服务实现...
- SpringBoot JPA动态查询与Specification详解:从基础到高级实战
-
一、JPA动态查询概述1.1什么是动态查询动态查询是指根据运行时条件构建的查询,与静态查询(如@Query注解或命名查询)相对。在业务系统中,80%的查询需求都是动态的,例如电商系统中的商品筛选、订...
- Java常用工具类技术文档(java常用工具类技术文档有哪些)
-
一、概述Java工具类(UtilityClasses)是封装了通用功能的静态方法集合,能够简化代码、提高开发效率。本文整理Java原生及常用第三方库(如ApacheCommons、GoogleG...
- Guava 之Joiner 拼接字符串和Map(字符串拼接join的用法)
-
Guave是一个强大的的工具集合,今天给大家介绍一下,常用的拼接字符串的方法,当然JDK也有方便的拼接字符串的方式,本文主要介绍guava的,可以对比使用基本的拼接的话可以如下操作...
- SpringBoot怎么整合Redis,监听Key过期事件?
-
一、修改Redis配置文件1、在Redis的安装目录2、找到redis.windows.conf文件,搜索“notify-keyspace-events”...
- 如何使用Python将多个excel文件数据快速汇总?
-
在数据分析和处理的过程中,Excel文件是我们经常会遇到的数据格式之一。本文将通过一个具体的示例,展示如何使用Python和Pandas库来读取、合并和处理多个Excel文件的数据,并最终生成一个包含...
- 利用Pandas高效处理百万级数据集,速度提升10倍的秘密武器
-
处理大规模数据集,尤其是百万级别的数据量,对效率的要求非常高。使用Pandas时,可以通过一些策略和技巧显著提高数据处理的速度。以下是一些关键的方法,帮助你使用Pandas高效地处理大型数据集,从而实...
- Python进阶-Day 25: 数据分析基础
-
目标:掌握Pandas和NumPy的基本操作,学习如何分析CSV数据集并生成报告。课程内容...
- Pandas 入门教程 - 第五课: 高级数据操作
-
在前几节课中,我们学习了如何使用Pandas进行数据操作和可视化。在这一课中,我们将进一步探索一些高级的数据操作技巧,包括数据透视、分组聚合、时间序列处理以及高级索引和切片。高级索引和切片...
- 原来这才是Pandas!(原来这才是薯片真正的吃法)
-
听到一些人说,Pandas语法太乱、太杂了,根本记不住。...
- python(pandas + numpy)数据分析的基础
-
数据NaN值排查,统计,排序...
- 利用Python进行数据分组/数据透视表
-
1.数据分组源数据表如下所示:1.1分组键是列名分组键是列名时直接将某一列或多列的列名传给groupby()方法,groupby()方法就会按照这一列或多列进行分组。按照一列进行分组...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)