python爬蟲Day 01

一 、爬蟲基本原理

    一、什麼是爬蟲?
        爬蟲就是爬取數據。

    二、什麼是互聯網?
        由一堆網絡設備,把一臺臺的
        計算機互聯到一塊兒稱之爲互聯網。

    三、互聯網創建的目的
        數據的傳遞與數據的共享。

    四、什麼是數據?
        例如:
            電商平臺的商品信息(淘寶、京東、亞馬遜)
            鏈家、自如租房平臺的房源信息
            股票證券投資信息(東方財富、雪球網)
            ...
            12306,票務信息(搶票)

    五、什麼是上網?
        普通用戶:
            打開瀏覽器
            ---> 輸入網址
            ---> 往目標主機發送請求
            ---> 返回響應數據
            ---> 把數據渲染到瀏覽器中

        爬蟲程序:
            模擬瀏覽器
            ---> 往目標主機發送請求
            ---> 返回響應數據
            ---> 解析並提取有價值的數據
            ---> 保存數據(文件寫入本地、
            持久化到數據庫中)

    六、爬蟲的全過程
        1.發送請求(請求庫: Requests/Selenium)
        2.獲取響應數據
        3.解析數據 (解析庫: BeautifulSoup4)
        4.保存數據 (存儲庫: 文件保存/MongoDB)

    總結: 咱們能夠把互聯網中的數據比喻成一座寶藏,
        爬蟲其實就是在挖取寶藏。

2、 requests請求庫html

    一、安裝與使用
        pip3 install requests

    二、分析請求流程(模擬瀏覽器)
        - 百度:
            1.請求url
                https://www.baidu.com/

            2.請求方式
                GET
                POST

            3.響應狀態碼

            4.請求頭信息
git

咱們爬蟲要用到的requests請求庫:github

安裝:web

1
pip3 install requests

 每次使用的時候在代碼前面加上數據庫

import requests
import requests
#爬蟲三部曲
# 1.發送請求
def get_page(url):
    response = requests.get(url)
    return response

# 2.解析數據
import re
def parse_index(html):
    #findall匹配全部
    # re.findall('正則匹配規則','匹配文本','匹配模式')
    #re.S:對所有文本進行搜索匹配
    detail_urls = re.findall('<div class="items"><a class="imglink" href="(.*?)"',html,re.S)
    return detail_urls
#解析詳情頁
def parse_detail(html):
    movie_url = re.findall('<source src="(.*?)">',html,re.S)
    if movie_url:
        return movie_url[0]  #只返回視頻的詳細網址

# 3.保存數據
import uuid
# uuid.uuid4()根據時間戳生成一段世界上惟一的字符串
def save_video(content):
    with open(f'{uuid.uuid4()}.mp4','wb') as f:
        f.write(content)
        print('視頻下載完畢')

#測試用例
if __name__ == '__main__':
    for line in range(6):
        url= f'http://www.xiaohuar.com/list-3-{line}.html'

        # 發送請求
        response = get_page(url)
        # print(response)

        # #返回響應狀態碼
        # print(response.status_code)

        # # 返回響應文本
        # print(response.text)

        #  解析主頁頁面
        detail_urls = parse_index(response.text)

        #循環遍歷詳情頁url
        for detail_url in detail_urls:
            print(detail_url)

            #往每一個詳情頁發送請求
            detail_response = get_page(detail_url)
            # print(response.text)

            #解析詳情頁獲取視頻url
            movie_url = parse_detail(detail_response.text)

            #判斷視頻存在url並打印
            if movie_url:
                print(movie_url)

                #往視頻url發送請求獲取視頻的二進制流
                movie_response=get_page(movie_url)

                #把視頻的二進制流傳給save_video函數保存到本地
                save_video(movie_response.content)

3、post請求自動登入github瀏覽器

"""
請求URL:https://github.com/login

請求頭:
    cookies
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36

請求體:
Form Data:
    commit: Sign in
    utf8: ✓
    authenticity_token: mOyooOxE7c/H7krQKEli+cwwy0+okuPOZqGEBSyu/kRfLHT2mNGu9RQcDnb1ovua1zQe3LOyYXxrWxFL+2aAcg==
    login: gr6g5r
    password: huhuhhge
    webauthn-support: supported
"""
import requests
import re
login_url = 'https://github.com/login'
login_header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
login_res = requests.get(url=login_url, headers=login_header)


# 解析提取token字符串
authenticity_token = re.findall(
    '<input type="hidden" name="authenticity_token" value="(.*?)"',
    login_res.text,
    re.S
)[0]
print(authenticity_token)

# 獲取login頁面的cookies信息
login_cookies = login_res.cookies.get_dict()

# 請求url
session_url = 'https://github.com/session'

# 請求頭信息
session_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
# 請求體信息
form_data = {
    "commit": "Sign in",
    "utf8": "",
    "authenticity_token": authenticity_token,
    "login": "*******",
    "password": "*******",
    "webauthn-support": "supported"
}

session_res = requests.post(
    url=session_url,
    headers=session_headers,
    cookies=login_cookies,
    data=form_data
)

with open('github.html', 'w', encoding='utf-8') as f:
    f.write(session_res.text)
相關文章
相關標籤/搜索