喜歡去知乎炸魚?用python吧

知乎高贊貼:node

有一雙大長腿是什麼體驗?python

有一副迷人的身材是什麼體驗?正則表達式

別用手機費勁的翻了,python幫你一臂之力網絡

import re
import requests
import os
import urllib.request
import ssl

from urllib.parse import urlsplit
from os.path import basename

# 全局禁用證書驗證
ssl._create_default_https_context = ssl._create_unverified_context

headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
    'Accept-Encoding': 'gzip, deflate'
}


def mkdir(path):
    if not os.path.exists(path):
        print('新建文件夾:', path)
        os.makedirs(path)
        return True
    else:
        print(u"圖片存放於:", os.getcwd() + os.sep + path)
        return False


def download_pic2(img_lists, dir_name):
    print("一共有{num}張照片".format(num=len(img_lists)))

    # 標記下載進度
    index = 1

    for image_url in img_lists:
        file_name = dir_name + os.sep + basename(urlsplit(image_url)[2])

        # 已經下載的文件跳過
        if os.path.exists(file_name):
            print("文件{file_name}已存在。".format(file_name=file_name))
            index += 1
            continue

        auto_download(image_url, file_name)

        print("下載{pic_name}完成!({index}/{sum})".format(pic_name=file_name, index=index, sum=len(img_lists)))
        index += 1


def auto_download(url, file_name):
    # 遞歸下載,直到文件下載成功
    try:
        urllib.request.urlretrieve(url, file_name)
    except urllib.request.ContentTooShortError:
        print("文件下載不完整,從新下載。")
        auto_download(url, file_name)
    except urllib.request.URLError:
        print("網絡鏈接出錯,嘗試從新下載。")
        auto_download(url, file_name)


def download_pic(img_lists, dir_name):
    print("一共有{num}張照片".format(num=len(img_lists)))
    for image_url in img_lists:
        response = requests.get(image_url, stream=True)
        if response.status_code == 200:
            image = response.content
        else:
            continue

        file_name = dir_name + os.sep + basename(urlsplit(image_url)[2])

        try:
            with open(file_name, "wb") as picture:
                picture.write(image)
        except IOError:
            print("IO Error\n")
            continue
        finally:
            picture.close()

        print("下載{pic_name}完成!".format(pic_name=file_name))


def get_image_url(qid, headers):
    # 利用正則表達式把源代碼中的圖片地址過濾出來
    # reg = r'data-actualsrc="(.*?)">'
    tmp_url = "https://www.zhihu.com/node/QuestionAnswerListV2"
    size = 10
    image_urls = []

    session = requests.Session()

    while True:
        postdata = {'method': 'next',
                    'params': '{"url_token":' + str(qid) + ',"pagesize": "10",' + '"offset":' + str(size) + "}"}
        page = session.post(tmp_url, headers=headers, data=postdata)
        ret = eval(page.text)
        answers = ret['msg']
        print(u"答案數:%d" % (len(answers)))

        size += 10

        if not answers:
            print("圖片 URL 獲取完畢, 頁數: ", (size - 10) / 10)
            return image_urls

        # reg = r'https://pic\d.zhimg.com/[a-fA-F0-9]{5,32}_\w+.jpg'
        imgreg = re.compile('data-original="(.*?)"', re.S)

        for answer in answers:
            tmp_list = []
            url_items = re.findall(imgreg, answer)

            for item in url_items:  # 這裏去掉獲得的圖片 URL 中的轉義字符'\\'
                image_url = item.replace("\\", "")
                tmp_list.append(image_url)

            # 清理掉頭像和去重 獲取 data-original 的內容
            tmp_list = list(set(tmp_list))  # 去重
            for item in tmp_list:
                if item.endswith('r.jpg'):
                    print(item)
                    image_urls.append(item)

        print('size: %d, num : %d' % (size, len(image_urls)))


if __name__ == '__main__':
    title = '擁有一副使人羨慕的好身材是怎樣的體驗?'
    question_id = 297715922

    # title = '身材好是一種怎樣的體驗?'
    # question_id = 26037846

    # title = '女孩子胸大是什麼體驗?'
    # question_id = 291678281

    # title = '女生什麼樣的腿是美腿?'
    # question_id = 310786985

    # title = '你的擇偶標準是怎樣的?'
    # question_id = 275359100

    # title = '什麼樣才叫好看的腿?'
    # question_id = 63727821

    # title = '身材對女生很重要嗎?'
    # question_id = 307403214

    # title = '女生腿長是什麼樣的體驗?'
    # question_id = 273711203

    # title = '女生腕線過襠是怎樣一種體驗?'
    # question_id = 315236887

    # title = '有着一雙大長腿是什麼感受?'
    # question_id = 292901966

    # title = '擁有一雙大長腿是怎樣的體驗?'
    # question_id = 285321190

    # title = '大胸女生如何穿衣搭配?'
    # question_id = 26297181

    # title = '胸大到底怎麼穿衣服好看?'
    # question_id = 293482116

    zhihu_url = "https://www.zhihu.com/question/{qid}".format(qid=question_id)
    path = str(question_id) + '_' + title
    mkdir(path)  # 建立本地文件夾
    img_list = get_image_url(question_id, headers)  # 獲取圖片的地址列表
    download_pic2(img_list, path)  # 保存圖片
相關文章
相關標籤/搜索