爬虫学习1
ztj100 2025-05-22 14:57 14 浏览 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)
相关推荐
- 其实TensorFlow真的很水无非就这30篇熬夜练
-
好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...
- 交叉验证和超参数调整:如何优化你的机器学习模型
-
准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...
- 机器学习交叉验证全指南:原理、类型与实战技巧
-
机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...
- 深度学习中的类别激活热图可视化
-
作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...
- 超强,必会的机器学习评估指标
-
大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...
- 机器学习入门教程-第六课:监督学习与非监督学习
-
1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...
- Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置
-
你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...
- 神经网络与传统统计方法的简单对比
-
传统的统计方法如...
- 自回归滞后模型进行多变量时间序列预测
-
下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...
- 苹果AI策略:慢哲学——科技行业的“长期主义”试金石
-
苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...
- 时间序列预测全攻略,6大模型代码实操
-
如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)