av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

基于Python模擬瀏覽器發送http請求

瀏覽:46日期:2022-07-06 09:57:38

1.使用 urllib2 實現

#! /usr/bin/env python# -*- coding=utf-8 -*- import urllib2url='https://www.baidu.com'req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11','Accept':'text/html;q=0.9,*/*;q=0.8','Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3','Accept-Encoding':'gzip','Connection':'close','Referer':None #注意如果依然不能抓取的話,這里可以設置抓取網站的host}req_timeout = 5req = urllib2.Request(url,None,req_header)resp = urllib2.urlopen(req,None,req_timeout)html = resp.read()print(html)

2.使用 requests 模塊

(1).get請求

#-*- coding:utf-8 -*-import requestsurl = 'https://www.baidu.com'payload = {'key1': 'value1', 'key2': 'value2'}r = requests.get(url, params=payload)print r.text

(2).post請求

#-*- coding:utf-8 -*-import requestsurl1 = 'http://www.exanple.com/login'#登陸地址url2 = 'http://www.example.com/main'#需要登陸才能訪問的地址data={'user':'user','password':'pass'}headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;', 'Accept-Encoding':'gzip', 'Accept-Language':'zh-CN,zh;q=0.8', 'Referer':'http://www.example.com/', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' }res1 = requests.post(url1, data=data, headers=headers)res2 = requests.get(url2, cookies=res1.cookies, headers=headers)print res2.content#獲得二進制響應內容print res2.raw#獲得原始響應內容,需要stream=Trueprint res2.raw.read(50)print type(res2.text)#返回解碼成unicode的內容print res2.urlprint res2.history#追蹤重定向print res2.cookiesprint res2.cookies['example_cookie_name']print res2.headersprint res2.headers['Content-Type']print res2.headers.get('content-type')print res2.json#講返回內容編碼為jsonprint res2.encoding#返回內容編碼print res2.status_code#返回http狀態碼print res2.raise_for_status()#返回錯誤狀態碼

(3).使用session對象的寫法

#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'http://www.exanple.com/login'#登陸地址url2 = 'http://www.example.com/main'#需要登陸才能訪問的地址data={'user':'user','password':'pass'}headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;', 'Accept-Encoding':'gzip', 'Accept-Language':'zh-CN,zh;q=0.8', 'Referer':'http://www.example.com/', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' }prepped1 = requests.Request('POST', url1, data=data, headers=headers).prepare()s.send(prepped1)'''也可以這樣寫res = requests.Request('POST', url1,data=data,headers=headers)prepared = s.prepare_request(res)# do something with prepped.body# do something with prepped.headerss.send(prepared)'''prepare2 = requests.Request('POST', url2, headers=headers).prepare()res2 = s.send(prepare2)print res2.content'''另一種寫法'''#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'http://www.exanple.com/login'#登陸地址url2 = 'http://www.example.com/main'#需要登陸才能訪問的頁面地址data={'user':'user','password':'pass'}headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;', 'Accept-Encoding':'gzip', 'Accept-Language':'zh-CN,zh;q=0.8', 'Referer':'http://www.example.com/', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' }res1 = s.post(url1, data=data)res2 = s.post(url2)print(resp2.content)

3.其他的一些請求方式

>>> r = requests.put('http://httpbin.org/put')>>> r = requests.delete('http://httpbin.org/delete')>>> r = requests.head('http://httpbin.org/get')>>> r = requests.options(http://httpbin.org/get)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 一级日批片 | 国产一区二区毛片 | 亚洲三区视频 | 日韩成人在线观看 | 国产精品久久久久久久久久尿 | 在线一区| 亚洲精品久久久蜜桃 | 99成人| 99视频免费播放 | 久久久久无码国产精品一区 | 色综网| 蜜桃av鲁一鲁一鲁一鲁 | 日本一区二区视频 | 日韩一二区 | 亚洲国产一区二区三区四区 | 成人a免费 | 一级a性色生活片久久毛片波多野 | 欧州一区二区三区 | 97福利在线 | 亚洲精品18| 很很干很很日 | 国产精品成人久久久久 | 国产午夜久久久 | 国产成人综合在线 | 综合久久综合久久 | 日韩精品久久一区二区三区 | 久久九 | 欧美日韩视频在线第一区 | 欧美视频成人 | 日韩精品专区在线影院重磅 | 国产高清在线 | 国产成人在线视频免费观看 | 国产午夜精品久久久久免费视高清 | 国产精品一区二区三区久久 | 亚洲女人天堂成人av在线 | 欧美国产中文字幕 | 久久一区二区三区四区 | 日韩中文久久 | 国产视频综合 | 午夜资源| 久久国产精品一区二区三区 |