爬虫笔记(一):实战登录古诗文网站
作者:mmseoamin日期:2024-01-23

需求:登录古诗文网站,账号+密码+图形验证码

第一:自己注册一个账号+密码哈

第二:图形验证码,需要一个打码平台(充钱,超能力power!)或者tesseract开源包,这两个用于自动识别图形验证码哈~

我用的是超级鹰,充了1块,有1000积分,一次10积分,初学者福音hhhhh

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第1张

在软件ID随便填一下软件名称和说明,获取软件key

然后点击首页,首页的菜单栏处有个开发文档,来到这个页面,然后找到python的Demo,get一个压缩包~

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第2张

压缩包里有一个chaojiying.py,把它复制到自己的项目里~

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第3张

第三:开始码

1. 调用chaojiying.py

把Chaojiying_Client中的三个参数替换成自己的即可,其中filePath是后续保存下来的验证码图片的路径~

from chaojiying import Chaojiying_Client
def getCode(filePath):
    c = Chaojiying_Client('超级鹰的账号', '超级鹰的密码', '软件Key')
    im = open(filePath, 'rb').read()
    return c.PostPic(im, 1902)['pic_str']
2. 获取验证码(还没到登录那一步哈)
headers = {
	'User-Agent': 'xxx'
}
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
# 发送请求
page_text = requests.get(url=url, headers=headers).text
# 获取页面源码
page_tree = etree.HTML(page_text)
# 获取验证码路径及验证码本体
# 通过page_tree.xpath('//*[@id="imgCode"]/@src')[0]可以获取验证码标签中的src属性,即验证码的路径
# 但验证码真实路径如下~
img_src = "https://so.gushiwen.cn" + page_tree.xpath('//*[@id="imgCode"]/@src')[0]
img_data = session.get(url=img_src, headers=headers).content
# 将验证码保存至同级code.jpg
with open('./code.jpg', 'wb') as f:
	f.write(img_data)
# 调用超级鹰,获取code
code = getCode('./code.jpg')

在浏览器按下F12,可以看到页面的源码信息,找到验证码对应的盒子,可以看到其src属性。标签右键,copy->copy xpath即可得到验证码路径,拼接在官网地址后就是代码中的img_src啦

至于xpath是啥,其实还没有学到那里,当当好在学习xpath的时候突然想打通一套登录流程,但大概理解xpath就是可以动态获取某个标签或元素属性的东西叭(超小声)

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第4张

3. 实战登录

在官网登录之后,打开开发者工具f12,找到如图所示的Payload,这就是访问登陆后的页面时的请求参数,共7个,其中只有__VIEWSTATE、__VIEWSTATEGENERATOR和code是动态变化的(碎碎念,一开始不知道__VIEWSTATE和__VIEWSTATEGENERATOR是啥,导致发送请求,一直提示错误,大哭出声)

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第5张

现在code已经有了,那__VIEWSTATE和__VIEWSTATEGENERATOR咋获取嘞

老规矩,获取这两玩应的xpath路径

还是,在源代码中找到这两,然后右键copy->copy xpath

爬虫笔记(一):实战登录古诗文网站,在这里插入图片描述,第6张

完整代码如下:

import re, os
import requests
from lxml import etree
from chaojiying import Chaojiying_Client
def getCode(filePath):
    c = Chaojiying_Client('超级鹰的账号', '超级鹰的密码', '软件Key')
    im = open(filePath, 'rb').read()
    return c.PostPic(im, 1902)['pic_str']
"""
    获取验证码
"""
if __name__ == "__main__":
    session = requests.Session()
    headers = {
        'User-Agent': 'xxx'
    }
    url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    # 先获取验证码
    page_text = requests.get(url=url, headers=headers).text
    page_tree = etree.HTML(page_text)
    # 获取__VIEWSTATE和__VIEWSTATEGENERATOR
    viewstate = page_tree.xpath('//*[@id="__VIEWSTATE"]')[0]
    viewagent = page_tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]')[0]
    img_src = "https://so.gushiwen.cn" + page_tree.xpath('//*[@id="imgCode"]/@src')[0]
    img_data = session.get(url=img_src, headers=headers).content
    with open('./code.jpg', 'wb') as f:
        f.write(img_data)
    # f.close()
    code = getCode('./code.jpg')
    print(code)
    # os.remove('code.jpg')
    params = {
        '__VIEWSTATE': viewstate,
        '__VIEWSTATEGENERATOR': viewagent,
        'from': 'http://so.gushiwen.cn/user/collect.aspx',
        'email': 'xxx',
        'pwd': 'xxx',
        'code': code,
        'denglu': '登录'
    }
    page_text = session.post(url=url, data=params, headers=headers).text
    with open('./gushi.html', 'w', encoding='utf-8') as f:
        f.write(page_text)

总结:生活越来越有判头了nie