1. 程式人生 > >路飛學院-Python爬蟲實戰密訓班-第1章

路飛學院-Python爬蟲實戰密訓班-第1章

bsp enc fin 以及 sign 模塊 nco comm soc

學習筆記:

通過本章的學習,學習到了requests和BeautifulSoup模塊的安裝及使用方法。以及爬取給類網站的方法和知識點。

1、requests和BeautifulSoup 安裝 pip install requests......

2、使用

import requests
from bs4 import BeautifulSoup

GET請求

r = requests.get(‘http://‘)
r.text 返回headers中的編碼解析的結果,可以通過r.encoding = ‘gbk‘來變更解碼方式
r.content返回二進制結果
r.json()返回JSON格式,可能拋出異常
r.status_code
r.raw返回原始socket respons,需要加參數stream=True

傳遞headers

headers ={
‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36‘,
‘Host‘:‘github.com‘
}

傳遞cookies

提交data
data = {
‘commit‘:‘Sign in‘,
‘utf8‘:‘?‘,
}

req_submit = requests.post(url=url,data=data,headers=headers1,cookies=cookies)

獲取頁面信息

req_submit.content.decode()

####################
加載要解析的文本內容
soup = BeautifulSoup("<html>data</html>",‘html.parser‘)
查找相應的標簽
soup_html = soup.find(name=‘html‘)

取出文本
soup_html.text

路飛學院-Python爬蟲實戰密訓班-第1章