未来做哪个网站能致富,无基础想学室内设计,厦门做企业网站,景观设计公司资质文章目录 一、抽屉半自动点赞二、xpath的使用三、动作链四、打码平台介绍超级鹰打码基本测试 五、自动登录超级鹰六、scrapy框架介绍安装创建爬虫项目 一、抽屉半自动点赞 登录抽屉账号保存cookiesimport timeimport jsonfrom selenium import webdriverfrom selenium.webdrive… 文章目录 一、抽屉半自动点赞二、xpath的使用三、动作链四、打码平台介绍超级鹰打码基本测试 五、自动登录超级鹰六、scrapy框架介绍安装创建爬虫项目 一、抽屉半自动点赞 登录抽屉账号保存cookiesimport timeimport jsonfrom selenium import webdriverfrom selenium.webdriver.common.by import Bybro webdriver.Chrome()bro.get(https://dig.chouti.com/)bro.implicitly_wait(10)bro.maximize_window()# 找到登录按钮,点击submit_btn bro.find_element(byBy.ID,valuelogin_btn)submit_btn.click()# 找到用户名密码框--输入用户名和密码username bro.find_element(By.CSS_SELECTOR,body div.login-dialog.dialog.animated2.scaleIn div div.login-body div.form-item.login-item.clearfix.phone-item.mt24 div.input-item.input-item-short.left.clearfix input)password bro.find_element(By.CSS_SELECTOR,body div.login-dialog.dialog.animated2.scaleIn div div.login-footer div.form-item.login-item.clearfix.mt24 div input.input.pwd-input.pwd-input-active.pwd-password-input)username.send_keys(xxxxx) # 手机号time.sleep(2)password.send_keys(xxxx) # 密码time.sleep(2)submit bro.find_element(By.CSS_SELECTOR,body div.login-dialog.dialog.animated2.scaleIn div div.login-footer div:nth-child(4) button)submit.click()input(等待人工确认登录----回车键后登录)# 登陆成功保存cookiecookies bro.get_cookies()print(cookies)with open(chouti.json,wt,encodingutf-8)as f:json.dump(cookies,f)time.sleep(5)bro.close()使用上面保存的cookie进行登录并点赞# 使用requests模块点赞把当前页面所有的文章点一遍import requestsfrom bs4 import BeautifulSoupimport json# 1.获取第一个的所有文章的id号headers {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36,Referer:https://dig.chouti.com/}res requests.get(https://dig.chouti.com/,headersheaders)# print(res.text)# 解析soupBeautifulSoup(res.text,lxml)div_list soup.find_all(namediv,class_link-item)# 获取本地cookiewith open(chouti.json,rt,encodingutf-8)as f:cookies json.load(f)requests_cookies{}for cookie in cookies:requests_cookies[cookie[name]]cookie[value]print(requests模块需要的cookie格式,requests_cookies)for div in div_list:article_id div.attrs.get(data-id)# print(article_id)# 要携带cookiedata {linkId:article_id}res requests.post(https://dig.chouti.com/link/vote,headersheaders,cookiesrequests_cookies,datadata)print(res.text)二、xpath的使用 # 语法格式如下记住这几个
1 标签名 # 找xml中所有这个标签
2 / # 只找一层]
3 // # 子子孙孙都会找
4 . # 从当前路径下
5 .. # 上一层
6 属性名 # 找有这个属性的标签
doc
htmlheadbase hrefhttp://example.com/ /titleExample website/title/headbodydiv idimagesa hrefimage1.html idid_a namelqzName: My image 1 br /img srcimage1_thumb.jpg //aa hrefimage2.htmlName: My image 2 br /img srcimage2_thumb.jpg //aa hrefimage3.htmlName: My image 3 br /img srcimage3_thumb.jpg //aa hrefimage4.html classliName: My image 4 br /img srcimage4_thumb.jpg //aa hrefimage5.html classli li-item nameitemsName: My image 5 br /img srcimage5_thumb.jpg //aa hrefimage6.html nameitemsspanh5test/h5/spanName: My image 6 br /img srcimage6_thumb.jpg //a/div/body
/htmlfrom lxml import etree
htmletree.HTML(doc) # 加载字符串
# htmletree.parse(search.html,etree.HTMLParser()) # 加载文件# 1 所有节点
print(html.xpath(//*))
print(html.xpath(/*))# 2 指定节点结果为列表
print(html.xpath(//head))# 3 子节点子孙节点
print(html.xpath(//div/a))
print(html.xpath(//body/a)) #无数据
print(html.xpath(//body//a))# 4 父节点
print(html.xpath(//body//a[hrefimage1.html]/..)) # 上一节点 div a..
print(html.xpath(//body//a[1]/..)) # 从1开始 第一个a标签..
# 也可以这样
print(html.xpath(//body//a[1]/parent::*)) # 找父亲---》父亲可以是任意标签
print(html.xpath(//body//a[1]/parent::div)) # 找父亲---》父亲可以是任意标签# 5 属性匹配
print(html.xpath(//a[hrefimage1.html])) # 属性匹配 标签为 a# 6 文本获取记住
print(html.xpath(//body//a[hrefimage1.html]/text())) # 内容获取 a标签内的内容[Name: My image 1 ]# 7 属性获取记住
print(html.xpath(//body//a/href)) # 拿所有a的href属性
print(html.xpath(//body//a[1]/href)) # 从1开始
# 注意从1 开始取不是从0
print(html.xpath(//body//a[1]/href))# 8 属性多值匹配
# a 标签有多个class类直接匹配就不可以了需要用contains
print(html.xpath(//body//a[classli])) # 有个类叫li的所有a标签因为这个a有俩类
print(html.xpath(//body//a[contains(class,li)])) # 属性多值匹配 匹配a标签内有class li的标签
print(html.xpath(//body//a[contains(class,li)]/text())) # 属性多值匹配 匹配a标签内有class li的标签的值# 9 多属性匹配
print(html.xpath(//body//a[contains(class,li) or nameitems])) # 多属性匹配 匹配a标签内有classli or nameitmes的内容
print(html.xpath(//body//a[contains(class,li) and nameitems]/text())) # 多属性匹配 匹配a标签内有classli and nameitmes的内容
print(html.xpath(//body//a[contains(class,li)]/text()))# 10 按序选择
print(html.xpath(//a[2]/text())) # 按序选择 查找第二个a标签的内容
print(html.xpath(//a[2]/href)) # 按序选择 查找第三个a标签的href内容
# 取最后一个
print(html.xpath(//a[last()]/href)) # 按序选择 查找最后一个a标签的href内容
# 位置小于3的
print(html.xpath(//a[position()3]/href)) # 按序选择 查找标签位置小于3的位置
# 倒数第二个
print(html.xpath(//a[last()-2]/href)) # 按序选择 查找倒数第二个a标签# 11 节点轴选择
# ancestor祖先节点
# 使用了* 获取所有祖先节点
print(html.xpath(//a/ancestor::*))
# # 获取祖先节点中的div
print(html.xpath(//a/ancestor::html))# attribute属性值
print(html.xpath(//a[1]/attribute::*)) # 获取第一个a标签的属性值
print(html.xpath(//a[1]/attribute::id))# child直接子节点
print(html.xpath(//a[1]/child::*)) # 获取第一个a标签的的子节点
print(html.xpath(//a[1]/child::img))
# descendant所有子孙节点
print(html.xpath(//a[6]/descendant::*)) # 获取第六个a标签的子节点
# following:当前节点之后所有节点
print(html.xpath(//a[1]/following::*)) # 获取第一个a标签之后的所有节点
print(html.xpath(//a[1]/following::*[1]/href)) # 获取第1个a标签之后的所有节点里面的第一个href里面所有的节点
# following-sibling:当前节点之后同级节点
print(html.xpath(//a[1]/following-sibling::*)) # 获取第一个a标签之后所有同级节点
print(html.xpath(//a[1]/following-sibling::a)) # 获取第一个a标签之后同级a节点
print(html.xpath(//a[1]/following-sibling::*[2])) # 获取第一个a标签之后所有同级节点第二个节点
print(html.xpath(//a[1]/following-sibling::*[2]/href)) # 获取第一个a标签之后所有同级节点第二个节点里面href的属性三、动作链 from selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素import timefrom selenium.webdriver.common.by import Bydriver webdriver.Chrome()driver.get(http://www.runoob.com/try/try.php?filenamejqueryui-api-droppable)driver.implicitly_wait(3)driver.maximize_window()try:driver.switch_to.frame(iframeResult) ##切换到iframeResultsourse driver.find_element(By.ID, draggable)target driver.find_element(By.ID, droppable)拿到actions对象后对象有很多方法1 把标签1 拖动到标签2上actions.drag_and_drop(标签1,标签2) 2 一点点滑动某个标签actions.click_and_hold(标签1).perform()actions.move_by_offset(x,y) # 把标签1 滑动x轴和y轴的距离3 滑动某个标签一些距离actions.drag_and_drop_by_offset(标签1,x,y)# 方式一基于同一个动作链串行执行# actions ActionChains(driver) # 拿到动作链对象# actions.drag_and_drop(sourse, target) # 把动作放到动作链中准备串行执行# actions.perform()# 方式二不同的动作链每次移动的位移都不同ActionChains(driver).click_and_hold(sourse).perform() # 鼠标点中源 标签 不松开distancetarget.location[x]-sourse.location[x]track 0while track distance:ActionChains(driver).move_by_offset(xoffset10, yoffset0).perform()track 10ActionChains(driver).release().perform()# 方式三# actions ActionChains(driver)# actions.drag_and_drop_by_offset(sourse,200,0).perform()time.sleep(5)finally:driver.close()四、打码平台
介绍 网站有验证码验证码破解-简单验证码字母数字组合---》免费的就能破---》ddddocr-https://www.jb51.net/article/249636.htm-复杂的收费---》打码平台--》花钱帮我们破解验证码把验证码图片传给它---它识别完--》返回结果---》根据复杂度收费-超级鹰:http://www.chaojiying.com/-下载SDK-云打码:https://zhuce.jfbym.com/price/超级鹰打码基本测试
#!/usr/bin/env python
# coding:utf-8import requests
from hashlib import md5class Chaojiying_Client(object):def __init__(self, username, password, soft_id):self.username usernamepassword password.encode(utf8)self.password md5(password).hexdigest()self.soft_id soft_idself.base_params {user: self.username,pass2: self.password,softid: self.soft_id,}self.headers {Connection: Keep-Alive,User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0),}def PostPic(self, im, codetype):im: 图片字节codetype: 题目类型 参考 http://www.chaojiying.com/price.htmlparams {codetype: codetype,}params.update(self.base_params)files {userfile: (ccc.jpg, im)}r requests.post(http://upload.chaojiying.net/Upload/Processing.php, dataparams, filesfiles, headersself.headers)return r.json()def PostPic_base64(self, base64_str, codetype):im: 图片字节codetype: 题目类型 参考 http://www.chaojiying.com/price.htmlparams {codetype: codetype,file_base64:base64_str}params.update(self.base_params)r requests.post(http://upload.chaojiying.net/Upload/Processing.php, dataparams, headersself.headers)return r.json()def ReportError(self, im_id):im_id:报错题目的图片IDparams {id: im_id,}params.update(self.base_params)r requests.post(http://upload.chaojiying.net/Upload/ReportError.php, dataparams, headersself.headers)return r.json()if __name__ __main__:# chaojiying Chaojiying_Client(超级鹰用户名, 超级鹰用户名的密码, 96001) #用户中心软件ID 生成一个替换 96001im open(a.jpg, rb).read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站价格体系 3.4版 print 后要加()#print chaojiying.PostPic(base64_str, 1902) #此处为传入 base64代码五、自动登录超级鹰
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image
from chaojiying import Chaojiying_Client
bro webdriver.Chrome()
bro.get(https://www.chaojiying.com/user/login/)
bro.implicitly_wait(10)
bro.maximize_window()# 截图全屏
bro.save_screenshot(main.png)
# 找到用户名和密码验证码输入框
username bro.find_element(By.CSS_SELECTOR,body div.wrapper_danye div div.content_login div.login_form form p.login_form_item input)
password bro.find_element(By.CSS_SELECTOR,body div.wrapper_danye div div.content_login div.login_form form p:nth-child(2) input)
code bro.find_element(By.XPATH,/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input)# 输入用户名密码验证码
username.send_keys()
time.sleep(2)
password.send_keys(!)
time.sleep(2)
# 破解验证码从截图中获取验证码
imgbro.find_element(By.XPATH,/html/body/div[3]/div/div[3]/div[1]/form/div/img)
# 找到img的大小和位置
location img.location
size img.size
print(大小是, img.size)
print(位置是, img.location)
# 获取图的 起始位置坐标 结束位置坐标
img_tu (int(location[x]), int(location[y]), int(location[x] size[width]), int(location[y] size[height]))
# 使用pillow根据坐标扣除验证码图片
img Image.open(./main.png)
# 抠图
fram img.crop(img_tu)
# 截出来的小图
fram.save(code1.png)# 调用超级鹰
# chaojiying Chaojiying_Client(17786176326,Mao0227!,958083)
# im open(code1.png, rb).read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# real_code chaojiying.PostPic(im, 1902)[pic_str] #1902 验证码类型 官方网站价格体系 3.4版 print 后要加()# 使用ddddocr
import ddddocr
ocr ddddocr.DdddOcr(oldTrue,show_adFalse)
# 第一个验证码截图保存verification_code_1.png
with open(./code1.png,rb)as f:image f.read()
real_code ocr.classification(image)code.send_keys(real_code)
time.sleep(5)# 找到登录按钮登录
submit bro.find_element(By.XPATH,/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input)
submit.click()
time.sleep(10)
bro.close()六、scrapy框架
介绍 前面讲的都是使用模块 做专业的爬虫可以使用框架Scrapy爬虫框架做爬虫用的东西都封装好了只需要在固定的位置写固定的代码即可 Scrapy一个开源和协作的框架其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛可用于如数据挖掘、监测和自动化测试等领域也可以应用在获取API所返回的数据或者通用的网络爬虫 安装 安装 win看人品linuxmac一点问题没有-pip install scrapy-装不上基本上是因为twisted装不了单独装1、pip3 install wheel #安装后便支持通过wheel文件安装软件wheel文件官网https://www.lfd.uci.edu/~gohlke/pythonlibs3、pip3 install lxml4、pip3 install pyopenssl5、下载并安装pywin32https://sourceforge.net/projects/pywin32/files/pywin32/6、下载twisted的wheel文件http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted7、执行pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl8、pip3 install scrapy在 D:\Python解释器对应的版本\Scripts 路径下 会有scrapy可执行文件-它等同于你安装了django--》多两个djagno-admin可执行文件创建爬虫项目 1.创建项目scrapy startproject 爬虫名称2.创建爬虫scrapy genspider cnblogs www.cnblogs.com # 这里是创建一个cnblogs的爬虫3.scrapy crawl cnblogs --nolog # --log 取消日志功能4.pycharm中运行新建run.pyfrom scrapy.cmdline import executeexecute([scrapy, crawl, cnblogs,--nolog])