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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6168|回复: 36
收起左侧

[Python 转载] 【python入门爬虫】爬取笔趣阁小说

  [复制链接]
a13381041 发表于 2020-5-9 17:59
本帖最后由 a13381041 于 2020-5-13 17:53 编辑

入门python练手的爬虫脚本,用于爬取笔趣阁小说(搜索,爬取小说属性,章节内容)
如果以后要写小说软件的可以借鉴
[Python] 纯文本查看 复制代码
import time
from bs4 import BeautifulSoup
import requests
import urllib.parse

#模拟小说搜索
def search(url):
    print("访问:"+url)
    #请求页面
    response = requests.get(url)
    #获取真实地址
    url = response.url
    #防止中文乱码,参考html的meta标签的content.charset属性
    response.encoding ='gbk'
    #获取html内容
    html_content = response.text
    #print(html_content)
    #转为bs
    soup=BeautifulSoup(html_content,"lxml")


    searchList = []
    #获取搜索的结果列表
    i=0
    if(len(soup.select('#main li'))<=0):
        if(soup.title.string =='笔趣阁'):
            return []
        else:
            name = soup.select('#info > h1')[0].string
            url = url
            author = soup.select('#info > p:nth-child(2) > a')[0].string
            novel = {"name":name,"url":url,"author":author}
            print(("""id:%d\t书名:%s\t作者:%s""" %(i, name, author)))
            searchList.append(novel)
            return searchList
    else:
        for child in soup.select('#main li'):
            name = child.select('.s2 a')[0].string
            url = child.select('.s2 a')[0].get('href')
            author = child.select('.s4')[0].string
            novel = {"name":name,"url":url,"author":author}
            searchList.append(novel)
            print(("""id:%d\t书名:%s\t作者:%s""" %(i, name, author)))
            i+=1
        return searchList

#爬取小说属性
def getNovelAtrr(url):
    print("访问:"+url)
    #请求页面
    response = requests.get(url)
    #防止中文乱码,参考html的meta标签的content.charset属性
    response.encoding ='gbk'
    #获取html内容
    html_content = response.text
    #print(html_content)
    #转为bs
    soup=BeautifulSoup(html_content,"lxml")

    #获取小说名,作者,简介,更新日期,字数,目录
    name = soup.select('#info h1')[0].string
    author = soup.select('#info > p:nth-child(2) > a')[0].string
    profile = soup.select('#intro')[0].text
    updata_wordnum = str(soup.select('#info > p:nth-child(4)')[0].text)
    index = updata_wordnum.find("[")
    lastindex = updata_wordnum.find("字")
    updataTime = updata_wordnum[5:int(index)]
    wordnum = updata_wordnum[int(index)+2:lastindex]
    catalogList = []
    for item in soup.select('#list > dl > dd a'):
        value = url + item.get("href")
        name = item.text
        catalog = {name:value}
        catalogList.append(catalog)
    Novel = {"name":name,"url":url,"profile":profile,"author":author,"updataTime":updataTime,"wordnum":wordnum,"catalogList":catalogList}
    return Novel

#打开小说章节
def openCatalog(url):
    print("访问:"+url)
    #请求页面
    response = requests.get(url)
    #防止中文乱码,参考html的meta标签的content.charset属性
    response.encoding ='gbk'
    #获取html内容
    html_content = response.text
    #print(html_content)
    #转为bs
    soup=BeautifulSoup(html_content,"lxml")
    content = soup.select('#content')[0].text
    print(content)

#选择搜索页的某本书,并访问某章节
def openNovel(id,searchList):
    if id>=len(searchList):
        print("没有这本书")
    else:
        Novel = getNovelAtrr(searchList[id]["url"])
        page = int(0)
        limit = int(10)
        while True:
            for i in range(limit):
                print(("""id:%d\t %s""" %(page*limit+i, Novel["catalogList"][page*limit+i])))
            cmdid = int(input("输入‘-1’上一页,输入‘-2’下一页,输入章节id访问章节:"))
            if(cmdid == -1):
                if(page>0):
                    page=page-1
            elif(cmdid == -2):
                if(page*limit<len(Novel["catalogList"])):
                    page=page+1
            elif(cmdid >= 0):
                url = Novel["catalogList"][cmdid]
                for key in url:
                    openCatalog(url[key])
                break

if __name__ == '__main__':
    searchUrl = 'https://www.52bqg.com/modules/article/search.php?searchkey='
    word = str(input("输入搜索关键字:"))
    #拼接链接,搜索关键字必须进行url转码
    url = searchUrl + urllib.parse.quote(word.encode('gbk'))
    searchList = search(url)
    if(len(searchList) == 0):
        print("检索失败!")
    else:
        openNovel(int(input("输入小说id:")),searchList)
        getNovelAtrr("https://www.52bqg.com/book_361/")


修改了查询结果唯一,没有显示查询结果的bug
添加了目录翻页功能

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
crazy19960202 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

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

ghoob321 发表于 2020-7-23 08:34
[Python] 纯文本查看 复制代码
        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/li/a/@href')
        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/div[2]/li/a/@href')
        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/div[2]/div[2]/li/a/@href')

        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/div[2]/div[2]/div[2]/div[2]/li/a/@href')
        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/li/a/@href')
        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/li/a/@href')


这一段怎么优化或简化
 楼主| a13381041 发表于 2020-8-7 10:22
ghoob321 发表于 2020-7-23 08:34
[mw_shl_code=python,true]        chapter_urls= html.xpath('//*[@id="chapter"]/div[3]/div[3]/ul/div[2 ...

通过浏览器的F12,然后选中想要爬取的节点,右键copy->copy selector可以复制到比较简洁的节点信息
woawapj 发表于 2020-5-9 18:09
bsjasd 发表于 2020-5-9 18:17
收藏备用
cskz008 发表于 2020-5-9 18:22
先收藏,等我学了Python再来康2333
往日那阵风 发表于 2020-5-9 19:04
收藏了 有时间看看
hj170520 发表于 2020-5-9 19:25
比我写的有质量多了
babyvoxs 发表于 2020-5-9 19:46
这个是PY几的,先学习学习一些基础的爬虫知识先,到时可以去爬爬其他的东西
poejie20200214 发表于 2020-5-9 20:04
感谢楼主分享
wanfon 发表于 2020-5-9 20:17
涨知识了,谢谢大神
开创者 发表于 2020-5-9 21:25
不错,测试可以用,不过刚安装这个软件,新手啊,完全看不懂,学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-5-15 21:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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