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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3315|回复: 14
收起左侧

[讨论] 某趣阁不能搜索了 换个小说网站爬

[复制链接]
lihu5841314 发表于 2021-10-10 08:48
[Asm] 纯文本查看 复制代码
#coding=gb2312
import logging
import  urllib.parse
import  requests,random,time
import parsel


headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4636.4 Safari/537.36'
}


def get_book_url(s):
    word = urllib.parse.quote(s, encoding="gb2312")  # 转码  和urllib.parse.quote() 一样
    url = f"https://www.qb5.la/modules/article/search.php?searchkey={word}&submit=%CB%D1%CB%F7"
    # 获得重定向的Location
    s = requests.session()
    logging.captureWarnings(True)
    resp = s.get(url=url, headers=headers, allow_redirects=False, verify=False)
    # 这是因为 requets 库自动处理了重定向请求了,默认是allow_redirects=True 是启动重定向,就在我们requests.get(url)中添加allow_redirects=False
    print(resp.status_code)  # 打印响应的状态码
    print(resp.headers["Location"])
    book_url = resp.headers["Location"]
    return  book_url

def html_response(url):
    time.sleep(random.randint(1,3))
    resp = requests.get(url=url, headers=headers)
    resp.encoding = resp.apparent_encoding
    selector = parsel.Selector(resp.text)
    return selector


def  get_char_url(url):
    selector = html_response(url)
    char_urls = selector.xpath("//div[@class='zjbox']//dd/a/@href").extract()
    char_titles = selector.xpath("//div[@class='zjbox']//dd/a/text()").extract()
    char_dic={}
    for char_title,char_url  in  zip(char_titles,char_urls):
        char_url = book_url + char_url
        char_dic[char_title]=char_url
        print(char_title,char_url)
    return  char_dic


def  char_book_down(dic):
    for char_title,char_url  in  dic.items():
        selector=html_response(char_url)
        content=selector.xpath("//div[@id='content']//text()").extract()
        char_content="".join([x.strip() for  x in content])
        path = s + ".txt"
        with open(path,"a",encoding="utf-8") as  f:
             f.write(char_title)
             f.write('\n')
             f.write(char_content)
             f.write('\n')
             print(char_title,"下载完成")
    print(s,"小说下载完成")



if __name__ == '__main__':
    s = input("请输入需要搜索的小说:")
    book_url = get_book_url(s)
    char_dic = get_char_url(book_url)
    char_book_down(char_dic)

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
lfm333 + 1 + 1 谢谢@Thanks!
默默看着你 + 1 + 1 我很赞同!

查看全部评分

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

ynthwf 发表于 2021-10-10 08:53
学习学习,
xytwlh 发表于 2021-10-10 09:04
baiqpl0123 发表于 2021-10-10 09:26
a2811260 发表于 2021-10-10 09:39
时头的好像还能用。可以试试
heykong 发表于 2021-10-10 09:42
感谢,学习了。
szhanyutian 发表于 2021-10-10 09:51
谢谢分享,学习学习
216288461 发表于 2021-10-10 10:36
换书香?
兮椿 发表于 2021-10-10 11:47
python?
lfm333 发表于 2021-10-10 11:59
感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-25 10:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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