獲取響應(yīng)信息
import requests
response = requests.get('http://www.baidu.com')
print(response.status_code) # 狀態(tài)碼
print(response.url) # 請(qǐng)求url
print(response.headers) # 響應(yīng)頭信息
print(response.cookies) # cookie信息
print(response.content) # bytes形式的響應(yīng)內(nèi)容
print(response.encoding) # 獲取響應(yīng)內(nèi)容編碼
response.encoding=”utf-8” # 指定響應(yīng)內(nèi)容編碼
print(response.text) # 文本形式的響應(yīng)內(nèi)容,response.content編碼后的結(jié)果
發(fā)送Get請(qǐng)求
不帶參數(shù)的Get請(qǐng)求
response = requests.get('http://www.baidu.com')
print(response.text)
帶參數(shù)的Get請(qǐng)求
直接寫在url后面
在url后面用?表示帶上參數(shù),每對(duì)參數(shù)用&分隔。如下url:
https://www.bilibili.com/vide...
注意:url最長(zhǎng)2048字節(jié),且數(shù)據(jù)透明不安全
作為字典參數(shù)傳入
data = {'name': 'xiaoming', 'age': 26}
response = requests.get('http://www.abcd.com', params=data)
print(response.text)
發(fā)送post請(qǐng)求
只能作為字典參數(shù)傳入,注意參數(shù)名字是data而不是params
data = {'name': 'xiaoming', 'age': 26}
response = requests.post('http://www.abcd.com', data=data)
print(response.text)
添加headers
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 ' /
'(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' /
'(KHTML, like Gecko) Version/5.1 Safari/534.50'
response = requests.get('http://www.baidu.com',headers=headers)
使用代理
proxy = {'http': '49.89.84.106:9999', 'https': '49.89.84.106:9999'}
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
req = requests.get(url, proxies=proxy, headers=heads)
print(req.text)
使用加密代理
from requests.auth import HTTPProxyAuth
proxies= {'http': '127.0.0.1:8888', 'https': '127.0.0.1:8888'}
auth = HTTPProxyAuth('user', 'pwd')
requests.get(url, proxies=proxies, auth=auth)
也可以這樣
proxies = {"http": "http://user:pass@10.10.1.10:3128/",}
req = requests.get(url, proxies=proxy, headers=heads)
Cookie
獲取Cookie
import requests
response = requests.get("http://www.baidu.com")
print(type(response.cookies))
# 把cookiejar對(duì)象轉(zhuǎn)化為字典
cookies = requests.utils.dict_from_cookiejar(response.cookies)
print(cookies)
使用Cookie
cookie = {"Cookie":"xxxxxxxx"}
response = requests.get(url,cookies=cookie)
Session
session = requests.Session()
session.get('http://httpbin.org/cookies/set/number/12345')
response = session.get('http://httpbin.org/cookies')
print(response.text)
限定響應(yīng)時(shí)間
from requests.exceptions import ReadTimeout
try:
response = requests.get('https://www.baidu.com', timeout=1)
print(response.status_code)
except :
print('給定時(shí)間內(nèi)未響應(yīng)')
解析JSON格式的響應(yīng)內(nèi)容
通過(guò)response.json()方法可以將為JSON格式的響應(yīng)內(nèi)容轉(zhuǎn)變?yōu)?a href="http://www.brongaenegriffin.com/tags/python/" target="_blank">Python的對(duì)象,json.loads(response.text)也能起到同樣的作用
response = requests.get('http://www.abcd.com')
print(response.text)
print(response.json())
print(type(response.json()))
想進(jìn)一步了解編程開(kāi)發(fā)相關(guān)知識(shí),與我一同成長(zhǎng)進(jìn)步,請(qǐng)關(guān)注我的公眾號(hào)“松果倉(cāng)庫(kù)”,共同分享宅&程序員的各類資源,謝謝?。?!
審核編輯 黃昊宇
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。
舉報(bào)投訴
-
python
+關(guān)注
關(guān)注
57文章
4876瀏覽量
90041 -
爬蟲
+關(guān)注
關(guān)注
0文章
87瀏覽量
8092
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
熱點(diǎn)推薦
Python數(shù)據(jù)爬蟲學(xué)習(xí)內(nèi)容
流程來(lái)實(shí)現(xiàn)的。這個(gè)過(guò)程其實(shí)就是模擬了一個(gè)人工瀏覽網(wǎng)頁(yè)的過(guò)程。Python中爬蟲相關(guān)的包很多:urllib、requests、bs4、scrapy、pyspider 等,我們可以按照reques
發(fā)表于 05-09 17:25
Python爬蟲與Web開(kāi)發(fā)庫(kù)盤點(diǎn)
beautifulsoup4、urllib2、lxml和requests是學(xué)習(xí)Python爬蟲必備的庫(kù),必須要掌握,當(dāng)然有的同學(xué)說(shuō)爬網(wǎng)頁(yè)不是也可以用正則表達(dá)式嗎,確實(shí)可以但是會(huì)很不方便,因?yàn)閎s4和lxml都有便捷
發(fā)表于 05-10 15:21
Python爬蟲初學(xué)者需要準(zhǔn)備什么?
了一個(gè)人工瀏覽網(wǎng)頁(yè)的過(guò)程。Python中爬蟲相關(guān)的包很多:urllib、requests、bs4、scrapy、pyspider 等,我們可以按照requests 負(fù)責(zé)連接網(wǎng)站,返回網(wǎng)
發(fā)表于 06-20 17:14
0基礎(chǔ)入門Python爬蟲實(shí)戰(zhàn)課
學(xué)習(xí)資料良莠不齊爬蟲是一門實(shí)踐性的技能,沒(méi)有實(shí)戰(zhàn)的課程都是騙人的!所以這節(jié)Python爬蟲實(shí)戰(zhàn)課,將幫到你!課程從0基礎(chǔ)入門開(kāi)始,受眾人群廣泛:如畢業(yè)大學(xué)生、轉(zhuǎn)行人群、對(duì)Python
發(fā)表于 07-25 09:28
Python爬蟲簡(jiǎn)介與軟件配置
Python爬蟲練習(xí)一、爬蟲簡(jiǎn)介1. 介紹2. 軟件配置二、爬取南陽(yáng)理工OJ題目三、爬取學(xué)校信息通知四、總結(jié)五、參考一、爬蟲簡(jiǎn)介1. 介紹網(wǎng)絡(luò)爬蟲
發(fā)表于 01-11 06:32
python網(wǎng)絡(luò)爬蟲概述
的數(shù)據(jù),從而識(shí)別出某用戶是否為水軍學(xué)習(xí)爬蟲前的技術(shù)準(zhǔn)備(1). Python基礎(chǔ)語(yǔ)言: 基礎(chǔ)語(yǔ)法、運(yùn)算符、數(shù)據(jù)類型、流程控制、函數(shù)、對(duì)象 模塊、文件操作、多線程、網(wǎng)絡(luò)編程 … 等(2). W3C標(biāo)準(zhǔn)
發(fā)表于 03-21 16:51
python爬蟲入門教程之python爬蟲視頻教程分布式爬蟲打造搜索引擎
本文檔的主要內(nèi)容詳細(xì)介紹的是python爬蟲入門教程之python爬蟲視頻教程分布式爬蟲打造搜索引擎
發(fā)表于 08-28 15:32
?30次下載
python爬蟲框架有哪些
本視頻主要詳細(xì)介紹了python爬蟲框架有哪些,分別是Django、CherryPy、Web2py、TurboGears、Pylons、Grab、BeautifulSoup、Cola。
Python爬蟲:使用哪種協(xié)議的代理IP最佳?
網(wǎng)絡(luò)大數(shù)據(jù)要抓取信息,大多需要經(jīng)過(guò)python爬蟲工作,爬蟲能夠幫助我們將頁(yè)面的信息抓取下來(lái)。
利用Python編寫簡(jiǎn)單網(wǎng)絡(luò)爬蟲實(shí)例
利用 Python編寫簡(jiǎn)單網(wǎng)絡(luò)爬蟲實(shí)例2
實(shí)驗(yàn)環(huán)境python版本:3.3.5(2.7下報(bào)錯(cuò)
發(fā)表于 02-24 11:05
?15次下載
如何解決Python爬蟲中文亂碼問(wèn)題?Python爬蟲中文亂碼的解決方法
如何解決Python爬蟲中文亂碼問(wèn)題?Python爬蟲中文亂碼的解決方法 在Python爬蟲過(guò)程
Python爬蟲之requests模塊教程
評(píng)論