模拟登录古诗网站

import requests
import json
from datetime import datetime
import base64
from lxml import etree

url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69"
}


def common_verify(image):
    # 数英汉字类型
    # 通用数英1-4位 10110
    # 通用数英5-8位 10111
    # 通用数英9~11位 10112
    # 通用数英12位及以上 10113
    # 通用数英1~6位plus 10103
    # 定制-数英5位~qcs 9001
    # 定制-纯数字4位 193
    # 中文类型
    # 通用中文字符1~2位 10114
    # 通用中文字符 3~5位 10115
    # 通用中文字符6~8位 10116
    # 通用中文字符9位及以上 10117
    # 中文字符 1~4位 plus 10118
    # 定制-XX西游苦行中文字符 10107
    # 计算类型
    # 通用数字计算题 50100
    # 通用中文计算题 50101
    # 定制-计算题 cni 452
    with open(image, "rb") as f:
        s = f.read()
    _custom_url = "http://api.jfbym.com/api/YmServer/customApi"
    _token = "云码平台个人中心获取token"
    _headers = {
        'Content-Type': 'application/json'
    }
    payload = {
        "image": base64.b64encode(s).decode(),
        "token": _token,
        "type": "10110"
    }
    resp = requests.post(_custom_url, headers=_headers, data=json.dumps(payload))
    print(resp.text)
    return resp.json()['data']['data']


def code_image_dispose():
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    # class定位
    img_url = "https://so.gushiwen.org" + tree.xpath("//div[@class='mainreg2']/div/img/@src")[0]
    # id定位
    # img_url = "https://so.gushiwen.org"+tree.xpath("//*[@id='imgCode']/@src")[0]
    img_date = requests.get(url=img_url, headers=headers).content
    img_path = f"./验证码-{str(datetime.now())}.jpg"
    with open(img_path, "wb") as fp:
        fp.write(img_date)
    print("验证码下载完成!!!")
    return img_path


def login(code):
    url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69"
    }
    param = {
        "__VIEWSTATE": "SKbAEdRDWUnaRTSumtFAV9HJtljHcbPgf3WfyI2dD898llqpaSWJYiW6F10ju6c5AxPwt7Cu0W5ayRqIJg2NNM59muUrUrzZUMFnvQg6JoUZcmZIu9c0gIaaBsI+RYiAH/wa1Imb6wjUhTTuMRz8aRsXmPw=",
        "__VIEWSTATEGENERATOR": "C93BE1AE",
        "from": "http://so.gushiwen.cn/user/collect.aspx",
        "email": "个人账号,
        "pwd": "密",
        "code": code,
        "denglu": "登录"
    }
    response = requests.get(url=url, params=param, headers=headers)
    print(response.status_code)


if __name__ == '__main__':
    imgPath = code_image_dispose()
    a = common_verify(imgPath)
    login(a)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题可以通过模拟登录古诗文网,并爬取个人收藏列表来解决。以下是实现的步骤: 1. 导入必要的库:requests、BeautifulSoup、re、json、time、selenium 2. 使用selenium模拟登录古诗文网,获取登录后的cookies 3. 使用requests库发送请求,带上cookies获取个人收藏列表页面的HTML内容 4. 使用BeautifulSoup解析HTML内容,获取收藏列表中每首古诗的链接 5. 使用requests库发送请求,获取每首古诗的具体页面HTML内容 6. 使用正则表达式或BeautifulSoup解析HTML内容,获取古诗的名字和作者 以下是代码实现的详细步骤: ```python import requests from bs4 import BeautifulSoup import re import json import time from selenium import webdriver # 设置登录账号和密码 username = "your_username" password = "your_password" # 使用selenium模拟登录获取cookies login_url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx" driver = webdriver.Chrome() driver.get(login_url) driver.find_element_by_id("email").send_keys(username) driver.find_element_by_id("pwd").send_keys(password) driver.find_element_by_id("denglu").click() time.sleep(5) cookies = driver.get_cookies() cookies_dict = {} for cookie in cookies: cookies_dict[cookie['name']] = cookie['value'] # 使用cookies发送请求获取收藏列表页面的HTML内容 collection_url = "https://so.gushiwen.cn/user/collect.aspx" response = requests.get(collection_url, cookies=cookies_dict) html = response.text # 使用BeautifulSoup解析HTML内容,获取收藏列表中每首古诗的链接 soup = BeautifulSoup(html, "html.parser") poems = soup.find_all("div", class_="sons") poem_links = [] for poem in poems: link = poem.find("a")["href"] poem_links.append("https://so.gushiwen.cn" + link) # 使用requests库发送请求,获取每首古诗的具体页面HTML内容,并使用正则表达式或BeautifulSoup解析HTML内容,获取古诗的名字和作者 for link in poem_links: response = requests.get(link, cookies=cookies_dict) html = response.text soup = BeautifulSoup(html, "html.parser") title = soup.find("div", class_="cont").find("h1").text.strip() author = soup.find("div", class_="cont").find("p", class_="source").text.strip() author = re.sub(r"\s+", " ", author).split(" ")[0][:-1] print(title, author) ``` 这样就可以成功爬取个人收藏列表中的古诗的名字和作者了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值