学习猿地网页爬取订单号
程序员文章站
2022-05-02 22:03:11
...
from lxml import etree
import requests
class LMonkey():
loginURL = 'https://www.lmonkey.com/login'
orderURL = 'https://www.lmonkey.com/my/order'
headers = {
'user-agent': # 自己设置即可
}
token = ''
request = None
orderNumber = 0
# 初始化
def __init__(self):
# 设置初始化请求对象
self.request = requests.session()
if self.getURL():
if self.postURL():
self.getOrderNumber()
# 1 登陆页面 获取token
def getURL(self):
request = self.request.get(url=self.loginURL, headers=self.headers)
if request.status_code == 200:
print('请求页面成功')
webpage = etree.HTML(request.text)
self.token = webpage.xpath("//input[@name='_token']/@value")[0]
print(self.token)
print('请求token成功')
return True
else:
print('请求失败')
# 2 模拟登录 设置cookie
def postURL(self):
_username = input("请输入用户名:")
_password = input("请输入密码:")
formData = {
'_token': self.token,
'username': _username,
'password': _password,
}
postResult = self.request.post(url=self.loginURL, headers=self.headers, data=formData)
if postResult.status_code == 200 or postResult.status_code == 302:
print("模拟登录成功")
return True
else:
print("模拟登录失败")
# 3 获取订单号码
def getOrderNumber(self):
getResult = self.request.get(url=self.orderURL, headers=self.headers)
if getResult.status_code == 200:
print("账户中心请求成功,正在解析")
webPage = etree.HTML(getResult.text)
od = webPage.xpath('//div[@class="avatar-content"]//small/text()')[0]
print(od)
self.orderNumber = od
# 类的实例化
_order = LMonkey()