1.下載安裝
(1)命令安裝方式
Windows:打開命令窗口行,直接運行包管理命令安裝
pip install requests or essy_install requests(簡易版)
unix/linux:打開 shell 窗口,運行包管理命令安裝 pip install requests
(2)離線安裝
下載離線安裝包 pip install requests-2.20.0-py2.py3-none-any.whl
官方網站 https://pypi.org/project/pip/
2.入門程序
# 引入依賴包
import requests
# 發送請求獲取服務器數據
response = requests.get("http://www.sina.com.cn")
# 得到數據
print(response.text)
- request.get :用於發送一個get請求給服務器,可以得到服務器的響應數據
- response.text :從響應對象中獲取文本數據
ps:自己try do it
3.請求對象:請求方式
HTTP1.1 規範中定義了 8 中請求操作方式:GET|POST|PUT|DELETE|OPTION|CONNECT|HEAD|PATCH 時下 WEB 項目比較流行的 RESTFul 請求方式有四種:GET|POST|PUT|DELETE 常規 WEB 項目最常用的請求方式有兩種:GET|POST
⚫ requests.request(method, url, **kwargs): ◼ 底層發送請求的操作方式
⚫ requests.get(url, params=None, **kwargs): ◼ 發送 GET 請求
⚫ requests.post(url, data=None, json=None, **kwargs): ◼ 發送 POST 請求
⚫ requests.put(url, data=None, **kwargs): ◼ 發送 PUT 請求
⚫ requests.delete(url, **kwargs): ◼ 發送 DELETE 請求
⚫ requests.patch(url, data=None, **kwargs): ◼ 發送 PATCH 請求
⚫ requests.options(url, **kwargs): ◼ 發送 OPTIONS 請求
⚫ requests.head(url, **kwargs): ◼ 發送 HEAD 請求
4.請求對象:GET參數傳遞
requests.get(url, params=None, **kwargs) #發送GET請求
@param url: get 請求服務器的地址
@param params: get 請求中附帶的參數
@param kwargs: 其他附帶參數,詳情參照 requests.request()源代碼
import requests
target_url = 'http://www.baidu.com/s' #定義目標url
data = {'wd': '魔道祖師'}
response = requests.get(target_url, params=data)
print(response.text)
⚫ get 請求方式要傳遞的參數是字典形式的數據,直接賦值給 params 參數即可
5.請求對象:POST參數的傳遞
requests.post(url, data=None, json=None, **kwargs) #發送POST的請求
@param url: post 請求服務器的 url 地址
@param data: post 請求中包含的常規參數數據
@param json: post 請求中包含的 like dict 數據/json 參數數據
# 引入依賴的模塊
import requests # 定義目標 url 地址
# url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' # 傳遞 post 中包含的參數
data = {
"i":"hello",
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"client":"fanyideskweb",
"salt":"1541660576025",
"sign":"4425d0e75778b94cf440841d47cc64fb",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_REALTIME",
"typoResult":"false",
} # 發送請求獲取服務器返回的響應數據
response = requests.post(url, data=data)
print(response.text)
6.請求對象:定製請求頭
requests模塊中的請求 底層都是通過requests.request(url,**kwargs)的headers參數進行操作
import requests # 引入依賴的模塊
from fake_useragent import UserAgent
ua = UserAgent() # 定義請求地址和請求頭數據
url = 'http://www.baidu.com/s'
headers = {'User-agent': ua.random}
param = {'wd': 'PYTHON 爬蟲'} # 發送請求獲取響應數據
response = requests.get(url, headers=headers)
print(response.text)
7.請求對象:cookie
WEB 開發中 cookie 經常被用於基於客戶端的狀態保持操作,所以在常規爬蟲處理過程中,cookie 的操作是最重要的操作之一。 可以直接通過定義一個字典數據傳遞給 requests 請求模塊的 cookies 參數,添加請求中的 cookie 數據 requests 中提供的 requests.cookies.RequestCookieJar()也可以在請求中添加處理 cookie 數據的操作並且更加適合跨域場景
8.響應對象
爬蟲從網絡上採集數據,採集到的數據主要區分爲如下幾種類型:文本數據、二進制數據 requests 模塊在響應對象中,針對返回的數據進行了不同的封裝處理
⚫ response.encoding: 設置響應數據的編碼,可以直接賦值 ◼ response.encoding = ‘utf-8’
⚫ response.text: 獲取響應對象中包含的文本數據
⚫ response.content: 獲取響應對象中包含的二進制數據
⚫ response.json(): 獲取響應對象中的 JSON 數據,數據必須正確解析,負責 raise ValueError
⚫ response.raw: 特殊情況下直接獲取底層 socket 數據流,此時請求中必須設置參數 stream=True 表示允許數據流處理
⚫ response.headers: 響應對象的響應頭數據
⚫ response.status_code: 響應對象中的響應狀態碼
⚫ response.cookie:獲取響應對象中包含的 cookie 數據
9.案例演示
登陸入口:http://www.renren.com/PLogin.do
登錄成功後,儘可能多的採集人人網用戶數據信息
提取人人網用戶數據的思路
1. 使用註冊賬號,登錄人人網[纔有權限查看個人信息]
2. 關注幾個網紅~爬蟲採集
採集1:他們關注的人|關注他的人 個人主頁 鏈接
採集2:當前用戶的個人信息[姓名、年齡、畢業學校、家鄉地址...]
3. 循環採集每個人的個人主頁鏈接改造後的 關注的人|關注他的人 個人主頁
Try DO It
"""
Version 1.1.0
Author lkk
Email [email protected]
date 2018-11-21 16:55
DESC 人人網信息爬取
"""
from selenium import webdriver
import requests
from lxml import html
from fake_useragent import UserAgent
import time, re
import utils1
ua = UserAgent()
# 人人網:用戶數據採集
login_url = "http://www.renren.com/PLogin.do"
# 賬號密碼
authentication = {'email': '1307170****', 'password': '*******'}
# 僞造請求頭
headers = {
'User-agent': ua.random,
}
# 發送請求,登錄網站
session = requests.Session()
response = session.post(login_url, data=authentication, headers=headers)
response.encoding = 'utf-8'
# 獲取個人信息:模擬
personal_url = 'http://follow.renren.com/list/968835593/pub/v7'
# 訪問該地址,需要攜帶身份信息:狀態保持使用的cookie數據
response2 = session.get(personal_url, headers=headers)
response2.encoding = 'utf-8'
# print()
# 處理結果數據
docs = html.fromstring(response2.text)
data = docs.xpath("//div[@class='module border']/ul[@id='follow_list']/li//div[@class='info']/a[@class='name']/@href")
name = docs.xpath("//a[@class='name']/text()")
for i in range(len(data)):
print(name[i], data[i])
# 從關注的網紅裏面爬取他們的粉絲信息
response3 = session.get(data[i], headers=headers)
response3.encoding = 'utf-8'
# print(response3.text)
docs1 = html.fromstring(response3.text)
link = docs1.xpath("//div[@class='has-friend']/h5/a[@class='title']/@href")
id = docs1.xpath("//button[@id='followAdd']/@data-id")
print(id[0])
school = docs1.xpath("//ul/li[@class='school']/span/text()")
sex = docs1.xpath("//ul/li[@class='birthday']/span[1]/text()")
bron = docs1.xpath("//ul/li[@class='birthday']/span[2]/text()")
hometown = docs1.xpath("//ul/li[@class='hometown']/text()")
school = school if len(school) > 0 else "暫無"
sex = sex if len(sex) > 0 else "暫無"
bron = bron if len(bron) > 0 else "暫無"
bron = bron if len(bron) > 0 else "暫無"
print(link[0])
time.sleep(5)
print(school, sex, bron, hometown)
# 自己關注網紅的粉絲
response4 = session.get(link[0], headers=headers)
response4.encoding = 'utf-8'
docs2 = html.fromstring(response4.text)
all_link = docs2.xpath("//li/div[@class='info']/a[@class='name']/@href")
fans_count = docs2.xpath("//ul/li[@class='select']/span/text()")
name = docs2.xpath("//div[@class='info']/a[@class='name']/text()")
fans_number = docs2.xpath("//div[@class='info']/p[@class='atten']/text()")
for info in range(len(name)):
print(name[info], fans_number[info], all_link[info])
# utils1.mysql(name[info], fans_number[info], all_link[info])
for k in range(20, int(fans_count[0]), 10):
long_index = 'http://follow.renren.com/list/'+id[0]+'/submore?visitId=968835593&offset='+str(k)+'&limit=10&requestToken=1198899405&_rtk=1336bf27'
# target_url = all_link[j] + '/profile/'
# print(target_url)
response5 = session.get(long_index, headers=headers)
response5.encoding = 'utf-8'
docs3 = html.fromstring(response5.text)
# print(response5.text)
fans_id = re.findall(r'"id":(\d+),', response5.text)
fans_name = re.findall(r'name":"(.*?)"', response5.text)
fans_fans_count = re.findall(r',"subscriberCount":(.*?),"', response5.text)
for n in range(len(fans_id)):
if int(fans_fans_count[n]) > 0:
print(fans_name[n], fans_fans_count[n], "http://www.renren.com/" + fans_id[n] + '/profile?v=info_timeline')
# utils1.mysql(fans_name[n], fans_fans_count[n], "http://www.renren.com/" + fans_id[n] + '/profile/')
else:
pass
# fans_school = docs3.xpath("//ul/li[@class='school']/span/text()")
# fans_sex = docs3.xpath("//ul/li[@class='birthday']/span[1]/text()")
# fans_bron = docs3.xpath("//ul/li[@class='birthday']/span[2]/text()")
# fans_hometown = docs3.xpath("//ul/li[@class='hometown']/text()")
# fans_school = fans_school if len(fans_school) > 0 else "暫無"
# fans_sex = fans_sex if len(fans_sex) > 0 else "暫無"
# fans_bron = fans_bron if len(fans_bron) > 0 else "暫無"
# fans_hometown = fans_hometown if len(fans_hometown) > 0 else "暫無"
# print(fans_school, fans_sex, fans_bron, fans_hometown)
# time.sleep(5)