python模拟浏览器请求

模拟浏览器请求

本人亲测

1:首先要有操作网站的账户密码

2:分析浏览器header标头

在这里插入图片描述
用代码代替浏览器访问

import urllib.request
import json
from urllib import parse

if __name__ == '__main__':
    # 模拟浏览器请求
    header = {
         'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    }
    url = "http://192.168.1.73:8080/web/ajax/login.jsp?username=%s&password=%s"%('035','123456')
    #发送请求
    c = urllib.request.Request(url, headers=header)
    #返回值response对象(包含read()、readinfo()、getheader(name)、getheaders()、fileno()等方法)
    a=urllib.request.urlopen(c)
    print(a)#返回值--------><http.client.HTTPResponse object at 0x7f4b783ed2e8>
    # 获取返回值read()、
    user = a.read()
    print(user)#返回值-------->b'{"msg":"","code":0,"count":1}'
    load_data = json.loads(user)
    print(load_data)#返回值-------->{'msg': '', 'code': 0, 'count': 1}
     # 获取cookie、值
    cookie = a.headers.get('Set-Cookie')

登录成功

3:代码查找功能

分析浏览器
在这里插入图片描述
用代码代替浏览器访问

	# 查找值
 	shuju = [201808060001, 1902110063]
    # 要操作多个功能,所以cookie很重要,他是身份的验证,从新定义header
    hea = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        'cookie': cookie
    }
    # 遍历集合值,模拟浏览器查找
    for name in shuju:
        url = 'http://192.168.1.73:8080/web/ajax/data2018_seach.jsp?order_num=%s&car_num='%(str(name))
        uhttp = urllib.request.Request(url,headers=hea)
        response = urllib.request.urlopen(uhttp)
        page= response.read()
        load_data = json.loads(page)
        print(load_data)

返回值:

[{…省略不写…}]

4:代码修改功能

分析浏览器
用前段Debugger模式分析出修改后的值
在这里插入图片描述
用代码代替浏览器访问

    for name in shuju:
        url = 'http://192.168.1.73:8080/web/ajax/data2018_seach.jsp?order_num=%s&car_num='%(str(name))
        uhttp = urllib.request.Request(url,headers=hea)
        response = urllib.request.urlopen(uhttp)
        page= response.read()
        load_data = json.loads(page)
        if len(load_data['data']) !=1:
            print("\033[1;45m 错误 %s---查到:%s条数据 \033[0m"%(name,len(load_data['data'])))
            break
        else:
            teepin = load_data['data'][0]
            list1=['voucher','num','member_name','f_date','d_date','car_num','dt','dt2','meb_name','meb_name_c','c_type','c1','c2','c3','c4','c_form_data']
            list2=['9-204-4',teepin['货单号'],teepin['客户名称'],teepin['发货日期'],teepin['发货日期'],teepin['车牌号码'],teepin['发货吨位'],teepin['到货吨位'],'',
                '***有限公司',teepin['经营模式'],'提货单','装车单','到货单','无榜单','提货单,装车单,到货单']
            url = "http://192.168.1.73:8080/web/ajax/data2018_edit.jsp?id=%s" % (teepin['索引'])
            datas = urllib.parse.urlencode(dict(zip(list1, list2))).encode("utf-8")    #列表转字典
            a = urllib.request.Request(url, data=datas, headers=header)
            a.add_header('cookie', cookie)
            respon = urllib.request.urlopen(a)
            result = respon.read()
            loaddata = json.loads(result)

            if(loaddata['code']!=0):
                print("\033[1;45m %s 修改失败 \033[0m" % (name))
                break
            print("%s:成功"%(name))

5:完整代码

import urllib.request
import json
from urllib import parse

if __name__ == '__main__':
    # 模拟浏览器请求
    header = {
         'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    }
    # 登陆获取cookie ['035','123456'是账户密码]
    url = "http://192.168.1.73:8080/web/ajax/login.jsp?username=%s&password=%s"%('035','123456')
    #发送请求
    c = urllib.request.Request(url, headers=header)
    #返回值response对象(包含read()、readinfo()、getheader(name)、getheaders()、fileno()等方法)
    a=urllib.request.urlopen(c)
    print(a)#返回值--------><http.client.HTTPResponse object at 0x7f4b783ed2e8>
    # 获取返回值read()、
    user = a.read()
    print(user)#返回值-------->b'{"msg":"","code":0,"count":1}'
    load_data = json.loads(user)
    print(load_data)#返回值-------->{'msg': '', 'code': 0, 'count': 1}
    shuju = [201808060001, 1902110063]
    # 获取cookie、值
    cookie = a.headers.get('Set-Cookie')
    # 要操作多个功能,所以cookie很重要,他是身份的验证,从新定义header
    hea = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        'cookie': cookie
    }
    # 遍历集合值,模拟浏览器查找
    for name in shuju:
        url = 'http://192.168.1.73:8080/web/ajax/data2018_seach.jsp?order_num=%s&car_num='%(str(name))
        uhttp = urllib.request.Request(url,headers=hea)
        response = urllib.request.urlopen(uhttp)
        page= response.read()
        load_data = json.loads(page)
        if len(load_data['data']) !=1:
            print("\033[1;45m 错误 %s---查到:%s条数据 \033[0m"%(name,len(load_data['data'])))
            break
        else:
            teepin = load_data['data'][0]
            list1=['voucher','num','member_name','f_date','d_date','car_num','dt','dt2','meb_name','meb_name_c','c_type','c1','c2','c3','c4','c_form_data']
            list2=['9-204-4',teepin['货单号'],teepin['客户名称'],teepin['发货日期'],teepin['发货日期'],teepin['车牌号码'],teepin['发货吨位'],teepin['到货吨位'],'',
                '*****有限公司',teepin['经营模式'],'提货单','装车单','到货单','无榜单','提货单,装车单,到货单']
            url = "http://192.168.1.73:8080/web/ajax/data2018_edit.jsp?id=%s" % (teepin['索引'])
            datas = urllib.parse.urlencode(dict(zip(list1, list2))).encode("utf-8")    #列表转字典
            a = urllib.request.Request(url, data=datas, headers=header)
            a.add_header('cookie', cookie)
            respon = urllib.request.urlopen(a)
            result = respon.read()
            loaddata = json.loads(result)

            if(loaddata['code']!=0):
                print("\033[1;45m %s 修改失败 \033[0m" % (name))
                break
            print("%s:成功"%(name))

注:
1:上述访问的网站是自己的,不存在于其他用途
2:仅作技术借阅

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小苹果也疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值