Xpath实战运用

xpath教程资源

xpath教程

脚本

import requests
from lxml import etree
#封装类
class GO():
loginurl = 'https://www.abc.com/login'
orderurl = 'https://www.abc.com/my/order'
headers = {'User-Agent':'   '}
# 请求对象
req = None
# token口令
token = ''
#订单号
ordercode = 0

#初始化方法
def _init_(self):
  #请求对象的初始化
  self.req = requests.session()

# get 登录页面的 获取 token
def getlogin(self):
  res = self.req.get(url = self.loginurl,headers = self.headers)
  if res.status_code == 200:
    print('get登录页面请求成功')
    html = etree.HTML(res.text)
    self.token = 'html.xpath(//input[@name="_token"]/@value')[0] #[0]作用:只显示数字结果
    print('token获取成功')
    return True  
  else:
    print('请求错误')
# post 请求登录,设置cookie
def postlogin(self):
  uname = input('用户名:')
  passw = input('用户名:')
  data = {
     'token':self.token,
     'username':uname,
     'password':passw
  }
#发起post请求
res = self.req.post(url=self.loginurl,headers=self.headers,data=data)
if res.status_code == 200 or res.status_code == 302:
  print('登录成功')
  return True
else 
   return False
# get 请求账户中心 获取默认单号
def getorder(self):
   res = self.req.get(url=self.orderurl,headers=self.headers)
   if res.status_code == 200:
      print('账户请求成功,正在解析数据')
      html = etree.HTML(res.text)
      r = html.xpath('//div[@class="balabal"]//small/text()')
      print(r)
      self.ordercode = r

obj = GO()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值