目录
前言
最近需要些简单的商品数据信息,听说淘宝反爬虫反的厉害,自己Python学习花的时间少,人菜的抠脚,就打起了京东的注意这gif去重后晃得我眼痛
一、需要的Python库安装
1.设置默认pip默认安装路径
安装前注意pip默认安装目录设置
查看位置为python目录下的Lib\site.py文件
设置参数(因为需要转义 写成r‘Path和单反斜的形式’ 或 ‘Path和双反斜的形式’)
USER_SITE=r‘D:\Python\Lib\site-packages’
USER_BASE =r‘D:\Python\Scripts’
↑site.py文件示意↑
如果你不想设置保存位置参数
①手动跳转到你的python安装目录下再使用pip命令安装(不然要么在c盘给你安装整个python,要么迫害你的c盘剩余容量)
②使用pip命令时使用 --target=Path 参数指定安装目录
↑pip安装示意图↑
2.下载浏览器驱动(以chrome为例)
打开谷歌浏览器访问下面的地址,查看谷歌浏览器版本
chrome://version/
↑查看chrome版本示意图↑
驱动下载链接
链接: 谷歌浏览器驱动下载.
下载相应版本的驱动(或者最接近的版本)
将终端放在chrome目录下(放其他地方注意改环境变量就行)
↑chrome目录↑
↑Win10设置驱动环境变量↑
二、使用步骤(代码以函数为单位分开展示)
1.引入库
代码如下:
import time
from urllib import request
import cv2
from selenium import webdriver
# from random import random
import pyautogui
from numpy import random
from lxml import etree
2.设置关键词和浏览器设置
代码如下:
#设置搜索关键词(根据需要修改)
keyword=['显卡','cpu','主板','内存','硬盘','散热器','机箱','外设','显示器','整机','笔记本电脑','平板','手机']
#对浏览器驱动进行设置
option=webdriver.ChromeOptions()
# option.add_argument('headless') #隐藏浏览器窗口(因为登录滑块需要使用鼠标,只能改为最小化)
option.add_argument("disable-blink-features=AutomationControlled") #去掉chrome的webdriver痕迹
browser = webdriver.Chrome(options=option)
驱动设置为不隐藏窗口;去除webdriver信息。
3.定义获取密码函数
代码如下:
#获取密码函数
def getLoginText():
f=open('D:\\PyCode\\txt\\JDLogin.txt', 'r', encoding='utf-8')
temp = f.readlines()
doc=[]
#去除元素中的换行转义
for t in temp:
t=t.strip('\n')
doc.append(t)
return doc
文件第一行是用户名,第二行是密码(没有任何前后缀。
直接在登录代码中明文定义账号密码不太好,容易泄露
4.定义获取图片信息,返回最佳匹配位置函数
代码如下:
#获取图片信息,返回最佳匹配位置
def findPic(target="img1.jpg", template="img2.png"):
# 读取图片
target_rgb = cv2.imread(target)
# 图片灰度化
target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
# 读取模块图片
template_rgb = cv2.imread(template, 0)
# 匹配模块位置
res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
# 获取最佳匹配位置
value = cv2.minMaxLoc(res)
# 返回最佳X坐标
return value[2][0]
5.定义滑动函数
代码如下:
#验证码滑动
def LoginSlide():
#获取滑块图像
target = browser.find_element_by_xpath('//div[@class="JDJRV-bigimg"]/img')
template = browser.find_element_by_xpath('//div[@class="JDJRV-smallimg"]/img')
# 获取模块的url路径
src1 = target.get_attribute("src")
src2 = template.get_attribute("src")
# 下载图片(参数二可以用 filename=‘想存放的目录位置+文件名’ 设置图片保存位置,只设置文件名默认保存在和代码统一目录下)
request.urlretrieve(src1,"img1.jpg")
request.urlretrieve(src2,"img2.png")
x = findPic()
w1 = cv2.imread('img1.jpg').shape[1]
w2 = target.size['width']
x = x / w1 * w2
# 按钮坐标(可以打开浏览器自己量一下,这里是1080p下大概坐标)
offset_x,offset_y = 1169,484
# pyautogui库操作鼠标指针
pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))
pyautogui.mouseDown()
offset_y += random.randint(9,19)
pyautogui.moveTo(offset_x + int(x * random.randint(15,25) / 20),offset_y,duration=0.28)
offset_y += random.randint(-9,0)
pyautogui.moveTo(offset_x + int(x * random.randint(17,23) / 20),offset_y,
duration=random.randint(20,31) / 100)
offset_y += random.randint(0,8)
pyautogui.moveTo(offset_x + int(x * random.randint(19,21) / 20),offset_y,
duration=random.randint(20,40) / 100)
offset_y += random.randint(-3,3)
pyautogui.moveTo(x + offset_x + random.randint(-3,3),offset_y,duration=0.5 + random.randint(-10,10) / 100)
offset_y += random.randint(-2,2)
pyautogui.moveTo(x + offset_x + random.randint(-2,2),offset_y,duration=0.5 + random.randint(-3,3) / 100)
pyautogui.mouseUp()
time.sleep(random.randint(2,5))
6.定义实现登录函数
代码如下:
#实现登录
def startLogin():
time.sleep(random.randint(2,5))
browser.maximize_window()
#点击账户登录
login=browser.find_element_by_xpath('//div[@class="login-tab login-tab-r"]/a')