学习日志练习,Python从起点爬取免费章节

上菜,啊,毕竟是自己全琢磨得一个,虽然可能大佬看起来太简单得操作,可是我硬是想了快8个小时,真香啊

import requests
from pyquery import PyQuery as pq
import json



def load(name):
    # 获取起点的搜索页面
    url_select = 'https://www.qidian.com/search?kw=' + name
    response_select = requests.get(url_select).content.decode('utf8')
    doc_select = pq(response_select)
    # clear 加了items变成了生成器,
    clear_select = doc_select('.book-mid-info h4 a').items()
    # 使用next进行迭代,由于只取第一个数据,所以不用for
    data_eid = next(clear_select).attr('data-bid')
    # print(data_eid)
    # 获取章节目录
    url_catalog = 'https://book.qidian.com/ajax/book/category?_csrfToken' \
                  '=KxOyODbbsZHWGtIfUsnDEqI9teZBDuUDC4QJ5YsZ&bookId=' + data_eid
    response_catalog = requests.get(url_catalog).content.decode('utf8')
    # 返回的是json格式其中包含起点所有的章节需要的id
    Json = json.loads(response_catalog)
    # 分析Json结构
    date = Json["data"]
    vs = date["vs"]
    for i in vs:
        cs = i["cs"]  # 包含正文的url的id以及章节名称
        vN = i["vN"]  # 分卷名称
        if vN == '作品相关' or vN == "VIP卷":
            continue
        else:
            for cU in cs:
                only = cU["cU"]  # cU是章节url的id
                cN = cU["cN"]    # 章节名称
                list_cU = {}
                if only not in list_cU:
                    list_cU[cN] = only  # 该字典是章节名称:章节ID
                    down(list_cU)


def down(ID_url):
    # key是章节名称,value章节id
    for key, value in ID_url.items():
        # 组合完成的每一章节的url
        urls = 'https://read.qidian.com/chapter/' + value
        response = requests.get(urls).content.decode('utf8')
        text = pq(response)
        # 使用PyQuery 筛选正文内容
        text_w = text(".read-content.j_readContent p")
        # 写入标题
        with open(r'D:\Users\MSI-PC\Desktop\123.txt', encoding='utf8', mode='a+') as f:
            f.write(str(key) + '\n')
        # 由于起点是每句一个

加属性 i获得是属性,需要加text方法获得内容 for i in text_w: with open(r'D:\Users\MSI-PC\Desktop\123.txt', encoding='utf8', mode='a+') as f: f.write(str(i.text) + '\n') if __name__ == '__main__': namebook = input("请输入书名:") load(namebook)

你可能感兴趣的:(学习笔记,爬虫)