1、requests庫
http協議中,最常用的就是GET方法: import requests response = requests.get('http://www.baidu.com') print(response.status_code) # 列印狀態碼 print(response.url) # 列印請求url print(response.headers) # 列印頭資訊 print(response.cookies) # 列印cookie資訊 print(response.text) #以文本形式列印網頁原始碼 print(response.content) #以位元組流形式列印
除此GET方法外,還有許多其他方法:
import requests requests.get('http://httpbin.org/get') requests.post('http://httpbin.org/post') requests.put('http://httpbin.org/put') requests.delete('http://httpbin.org/delete') requests.head('http://httpbin.org/get') requests.options('http://httpbin.org/get')
2、BeautifulSoup庫
BeautifulSoup庫主要作用:
經過Beautiful庫決議后得到的Soup檔案按照標準縮進格式的結構輸出,為結構化的資料,為資料過濾提取做出準備,
Soup檔案可以使用find()和find_all()方法以及selector方法定位需要的元素:
1. find_all()方法
soup.find_all('div',"item") #查找div標簽,
find_all(name, attrs, recursive, string, limit, **kwargs)
@PARAMS:
name: 查找的value,可以是string,list,function,真值或者re正則運算式
attrs: 查找的value的一些屬性,class等,
recursive: 是否遞回查找子類,bool型別
string: 使用此引數,查找結果為string型別;如果和name搭配,就是查找符合name的包含string的結果,
limit: 查找的value的個數
**kwargs: 其他一些引數
2. find()方法
find()方法與find_all()方法類似,只是find_all()方法回傳的是檔案中符合條件的所有tag,是一個集合,find()方法回傳的一個Tag
3、select()方法
soup.selector(div.item > a > h1) 從大到小,提取需要的資訊,可以通過瀏覽器復制得到,
select方法介紹
示例:
<html><head><title>The Dormouse's story</title></head>
<body>
<p name="dromouse"><b>The Dormouse's story</b></p>
<p >Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p >...</p>
"""
在寫css時,標簽名不加任何修飾,類名前加點,id名前加 #,我們可以用類似的方法來篩選元素,用到的方法是soup.select(),回傳型別是list,
(1).通過標簽名查找
print(soup.select('title')) #篩選所有為title的標簽,并列印其標簽屬性和內容 # [<title>The Dormouse's story</title>] print(soup.select('a')) #篩選所有為a的標簽 # [<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a href="http://example.com/lacie" id="link2">Lacie</a>, <a href="http://example.com/tillie" id="link3">Tillie</a>] print(soup.select('b')) #篩選所有為b的標簽,并列印 # [<b>The Dormouse's story</b>]
(2).通過類名查找
print soup.select('.sister') #查找所有class為sister的標簽,并列印其標簽屬性和內容 # [<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a href="http://example.com/lacie" id="link2">Lacie</a>, <a href="http://example.com/tillie" id="link3">Tillie</a>]
(3).通過id名查找
print soup.select('#link1') #查找所有id為link1的標簽,并列印其標簽屬性和內容 #[<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
(4).組合查找
組合查找即和寫class檔案時,標簽名與類名、id名進行的組合原理是一樣的,例如查找p標簽中,id等于link1的內容,二者需要空格分開,
print soup.select('p #link1') #[<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
直接子標簽查找
print soup.select("head > title") #[<title>The Dormouse's story</title>]
(5).屬性查找
查找時還可以加入屬性元素,屬性需要用中括號括起來,注意屬性和標簽屬于同一節點,所以中間不能加空格,否則會無法匹配到,
print soup.select("head > title") #[<title>The Dormouse's story</title>] print soup.select('a[href="http://example.com/elsie"]') #[<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
屬性仍然可以與上述查找方式組合,不在同一節點的空格隔開,同一節點的不加空格,
print soup.select('p a[href="http://example.com/elsie"]') #[<a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
BeautifulSoup庫例句:
from bs4 import BeautifulSoup import requests f = requests.get(url,headers=headers) soup = BeautifulSoup(f.text,'lxml') for k in soup.find_all('div',class_='pl2'): #找到div并且class為pl2的標簽 b = k.find_all('a') #在每個對應div標簽下找a標簽,會發現,一個a里面有四組span n.append(b[0].get_text()) #取第一組的span中的字串
3、lxml庫
lxml 是 一個HTML/XML的決議器,主要的功能是如何決議和提取 HTML/XML 資料,
示例如下:
# 使用 lxml 的 etree 庫 from lxml import etree text = ''' <div> <ul> <li ><a href="https://www.cnblogs.com/v-fan/p/link1.html">first item</a></li> <li ><a href="https://www.cnblogs.com/v-fan/p/link2.html">second item</a></li> <li ><a href="https://www.cnblogs.com/v-fan/p/link3.html">third item</a></li> <li ><a href="https://www.cnblogs.com/v-fan/p/link4.html">fourth item</a></li> <li ><a href="https://www.cnblogs.com/v-fan/p/link5.html">fifth item</a> # 注意,此處缺少一個 </li> 閉合標簽 </ul> </div> ''' #利用etree.HTML,將字串決議為HTML檔案 html = etree.HTML(text) # 按字串序列化HTML檔案 result = etree.tostring(html) print(result)
輸出結果如下:
<html><body> <div> <ul> <li class="item-0"><a href=https://www.cnblogs.com/v-fan/p/"link1.html">first item</a></li> <li class="item-1"><a href=https://www.cnblogs.com/v-fan/p/"link2.html">second item</a></li> <li class="item-inactive"><a href=https://www.cnblogs.com/v-fan/p/"link3.html">third item</a></li> <li class="item-1"><a href=https://www.cnblogs.com/v-fan/p/"link4.html">fourth item</a></li> <li class="item-0"><a href=https://www.cnblogs.com/v-fan/p/"link5.html">fifth item</a></li> </ul> </div> </body></html>
可以看到,lxml會自動修改HTML代碼,例子中不僅補全了li標簽,還添加了body,html標簽,
4、json庫
| 函式 | 描述 |
|---|---|
| json.dumps | 將python物件編碼成JSON字串 |
| json.loads | 將已編碼的JSON字串決議為python物件 |
1. json.dumps的使用
#!/usr/bin/python import json data = [ { 'name' : '張三', 'age' : 25}, { 'name' : '李四', 'age' : 26} ] jsonStr1 = json.dumps(data) #將python物件轉為JSON字串 jsonStr2 = json.dumps(data,sort_keys=True,indent=4,separators=(',',':')) #讓JSON資料格式化輸出,sort_keys:當key為文本,此值為True則按順序列印,為False則隨機列印 jsonStr3 = json.dumps(data, ensure_ascii=False) #將漢字不轉換為unicode編碼 print(jsonStr1) print('---------------分割線------------------') print(jsonStr2) print('---------------分割線------------------') print(jsonStr3)
輸出結果:
[{"name": "\u5f20\u4e09", "age": 25}, {"name": "\u674e\u56db", "age": 26}] ---------------分割線------------------ [ { "age":25, "name":"\u5f20\u4e09" }, { "age":26, "name":"\u674e\u56db" } ] ---------------分割線------------------ [{"name": "張三", "age": 25}, {"name": "李四", "age": 26}]
2. json.loads的使用
#!/usr/bin/python import json data = [ { 'name' : '張三', 'age' : 25}, { 'name' : '李四', 'age' : 26} ] jsonStr = json.dumps(data) print(jsonStr) jsonObj = json.loads(jsonStr) print(jsonObj) # 獲取集合第一個 for i in jsonObj: print(i['name'])
輸出結果為:
[{"name": "\u5f20\u4e09", "age": 25}, {"name": "\u674e\u56db", "age": 26}] [{'name': '張三', 'age': 25}, {'name': '李四', 'age': 26}] 張三 李四`
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176260.html
標籤:Python
