一呐伞、網(wǎng)絡(luò)數(shù)據(jù)采集
涉及內(nèi)容:
數(shù)據(jù)庫敌卓、網(wǎng)絡(luò)服務(wù)器、HTTP協(xié)議伶氢、HTML語言趟径、網(wǎng)絡(luò)安全、圖像處理癣防、數(shù)據(jù)科學(xué)等內(nèi)容蜗巧。
二、開發(fā)環(huán)境
- Python版本:Python3.5
- 操作系統(tǒng) : Win10
- IDE : 系統(tǒng)IDLE 蕾盯、Pycharm
三幕屹、相關(guān)庫
- urllib
- requests
- selenium
- re
- BeautifulSoup
- selenium
- threading 、 muitiprocess
- mysql.connector
- xlsxwriter
- scrapy
四级遭、步驟工具
(一) 獲取網(wǎng)頁望拖,請求數(shù)據(jù)
- urllib
- requests
- selenium
1、
from urllib.request import urlopen
html = urlopen(url)
# html.read() 為未處理的二進(jìn)制網(wǎng)頁源代碼
soup = BeautifulSoup(html.read(), 'lxml') #網(wǎng)頁源代碼
#加不加 .read 好像沒差
2挫鸽、
import requests
html = requests.get(url, headers=headers)
# html.text 為未處理的網(wǎng)頁源代碼
soup = BeautifulSoup(html.text, 'lxml')
# 得加 .text
3说敏、
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get(url)
driver.page_source #為未處理的網(wǎng)頁源代碼
(二) 提取內(nèi)容
- re
- BeautifulSoup
- selenium
1、
re 匹配
2丢郊、
BeautifulSoup
find盔沫、 find_all医咨、 select
網(wǎng)頁標(biāo)簽,selector
3架诞、
find_elements...
網(wǎng)頁標(biāo)簽拟淮,selector,xpath
(三) 下載內(nèi)容
- open...write
- urllretrieve
- requests
下載圖片等文件
1侈贷、
from urllib.request import urlretrieve
urlretrieve(url, filename=None, reporthook=None, data=None)
2惩歉、
html = urlopen(imageUrl)
data = html.read()
f= open(fileName,'wb')
f.write(data)
f.close()
3、
import requests
picture = requests.get(url, headers=headers)
if picture.status_code == 200:
open(path, 'wb').write(picture.content)
(四) 儲存
- csv
- xlsxwriter
- mysql.connector
- pymongdb
(五) 模擬瀏覽器
selenium
splinter
(六) 中級爬蟲框架
- scrapy