Python-數(shù)據(jù)爬取

數(shù)據(jù)購買

數(shù)據(jù)公司
數(shù)據(jù)交易所

爬取數(shù)據(jù)

數(shù)據(jù)獲取
數(shù)據(jù)清洗
第三方框架:scrapy、scrapy-redis
反爬蟲- 反反爬蟲

網(wǎng)絡部分

HTTP協(xié)議
HTTPS

網(wǎng)絡爬蟲

爬取數(shù)據(jù)的原理:使用程序批量獲取數(shù)據(jù)-->用程序模擬一個瀏覽器,發(fā)送HTTP請求(GET、POST)
HTTP原理:(BS結(jié)構)
客戶端請求 -- 服務器響應

url

protocol hostname post path
e.g
https://www.baidu.com/s?ie=UTF-8&wd=scrapy%E6%A1%86%E6%9E%B6
url?參數(shù):【ie=UTF-8&wd=scrapy%E6%A1%86%E6%9E%B6】
ie 參數(shù)名、UTF-8參數(shù)值(兩者間由=分割,不同的參數(shù)間由&分割)
注:%E6%A1%86%E6%9E%B6對中文的加密轉(zhuǎn)碼
中文編碼、解碼工具

  • request
    相當于發(fā)送一個字符串(帶格式的)
    請求行、請求頭、空行、數(shù)據(jù)
  • post

方式由服務器決定:
若http協(xié)議中包含數(shù)據(jù)部分,那么請求方式為post【對數(shù)據(jù)要求較高的情況下】
url后面參數(shù) - get方式提交(無請求體)
(爬蟲為方便不適用accept encoding,直接使用明文傳遞)

響應:
狀態(tài)行、消息報頭、空行、響應正文
狀態(tài)(e.g.200正常、404)

import urllib.request
import urllib.parse#只能加密字典
kw = {
    "kw":"北京航空航天大學"
}
url = "http://tieba.baidu.com/f?"
url += urllib.parse.urlencode(kw)
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
content = response.read().decode("utf-8")
with open("demo1.html","a",encoding = "utf-8") as f:
    f.write(content)
import urllib.request
import urllib.parse#只能加密字典
keyword = input("請輸入貼吧名")
kw = {
    "kw":keyword
}
startIndex = int(input("起始頁"))
endIndex = int(input("結(jié)束頁"))
url = "http://tieba.baidu.com/f?"
url += urllib.parse.urlencode(kw)
for index in range(startIndex, endIndex+1):
    pn = str((index-1)*50)
    url += "&pn="+ pn
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    content = response.read().decode("utf-8")
    with open(f"E:\py\demo{index}.html","a",encoding = "utf-8") as f:
        f.write(content)

xml--xpath


image.png
image.png

屬性過濾:

  • id
    xpath正確但python中不返回正確內(nèi)容:反爬蟲
    可能是js生成
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容