zoukankan      html  css  js  c++  java
  • request发送带headers和带参数的请求

    如果不改变header,往往只能获取到很少一部分的content。所以我们要改变header

    import requests
    respones = requests.get("https://www.baidu.com")
    print(respones.status_code)
    print(respones.headers)
    print(respones.request.headers)
    print(respones.content.decode())
    print("更改headers后")
    headers1 = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) App1eWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36"}
    respones1 = requests.get("https://www.baidu.com",headers = headers1)
    print(respones1.content.decode())
    
    

    带参数

    import requests
    headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}
    p = {"wd":"传智播客"}
    url_temp = "https://www.baidu.com/s?"
    r = requests.get(url_temp,headers=headers,params=p)
    print(r.status_code)
    print(r.request.url)
    
    

    也可以这样

    import requests
    headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}
    url = "https://www.baidu.com/s?wd={}".format("传智播客")
    r = requests.get(url,headers=headers)
    print(r.status_code)
    print(r.request.url)
    
    

    运行结果

  • 相关阅读:
    后缀自动机学习小记
    [bzoj4524] [loj#2047] [Cqoi2016] 伪光滑数
    [bzoj4825] [loj#2018] [Hnoi2017] 单旋
    [bzoj4571] [loj#2016] [Scoi2016] 美味
    [bzoj4569] [loj#2014] [Scoi2016] 萌萌哒
    [bzoj4568] [loj#2013] [Scoi2016] 幸运数字
    [bzoj4567] [loj#2012] [SCOI2016] 背单词
    deque双向队列
    STL_vector
    qsort与sort()
  • 原文地址:https://www.cnblogs.com/SunChuangYu/p/13229523.html
Copyright © 2011-2022 走看看