吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 509|回复: 3
收起左侧

[学习记录] python爬取某点免费章节

[复制链接]
DoveNest 发表于 2024-1-21 15:55
本帖最后由 苏紫方璇 于 2024-1-21 16:36 编辑

[Python] 纯文本查看 复制代码
import re
import time

import lxml
from bs4 import BeautifulSoup

import requests


def get_description(book_url,headers=None):
    text_id = book_url.split('/')[-2]
    print(text_id)
    headers = {
        'Host': "www.qidian.com",
        "Referer": headers,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
    }
    resp = requests.get(url, headers=headers)
    description = re.search(r'<main id="c-{0}" data-type="cjk".*?<p>(.*?)</main>'.format(text_id), resp.text).group(1)
    print(description)
    bs4 = BeautifulSoup(resp.text, 'lxml')
    result = bs4.findAll('a', class_='nav-btn')
    for i in result:
        print(i.text)
        if i.text == '下一章':
            next_url = i['href'].split('//')[1]
    # with open('my_test.html', "w") as f:
    #     f.write(description)
    return 'https://'+next_url


if __name__ == '__main__':
    url = 'https://www.qidian.com/chapter/1038476773/774184394/'
    for i in range(10):
        if i==0:
            headers='https://www.qidian.com/chapter/1038476773'
        else:
            headers=url
        url = get_description(url,headers=headers)
        time.sleep(3)

免费评分

参与人数 1热心值 +1 收起 理由
zql961213wgh + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

losingstars 发表于 2024-1-21 17:27
在起点找篇文试下。
sai609 发表于 2024-1-21 20:58
hqt 发表于 2024-1-21 22:41
当发起一个请求的时候header的host不是已经设置为了对应域名吗?为什么要单独写进header
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 20:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表