python模拟浏览器请求

模拟浏览器请求

本人亲测

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

2:分析浏览器header标头

python模拟浏览器请求
用代码代替浏览器访问

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:代码查找功能

分析浏览器
python模拟浏览器请求
用代码代替浏览器访问

	# 查找值
 	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模式分析出修改后的值
python模拟浏览器请求
用代码代替浏览器访问

    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:仅作技术借阅

上一篇:python 爬虫 urllib请求对象的定制


下一篇:python实现简单爬虫功能