路飞学院python官网-路飞学院-Python爬虫实战密训班-第1章
學習筆記:
通過本章的學習,學習到了requests和BeautifulSoup模塊的安裝及使用方法。以及爬取給類網(wǎng)站的方法和知識點。
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,需要加參數(shù)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()
####################
加載要解析的文本內(nèi)容
soup = BeautifulSoup("data","html.parser")
查找相應的標簽
soup_html = soup.find(name="html")
取出文本
soup_html.text
總結
以上是生活随笔為你收集整理的路飞学院python官网-路飞学院-Python爬虫实战密训班-第1章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深解微服务架构:从过去,到未来
- 下一篇: 感知机模型的对偶形式[转载]