python学习分享笔趣阁小说全本下载工具爬虫源码
导入模块importrequestsimportparselfromlxmlimportetreeimportreimportpandasaspdimportdatetimeimporttimefromtqdmimporttqdmimportprettytableaspt请求头headers{UserAgent:Mozilla5。0(WindowsNT6。1;WOW64)AppleWebKit537。36(KHTML,likeGecko)Chrome78。0。3904。108Safari537。36}currtimedatetime。datetime。now()timesdatetime。datetime。strftime(currtime,YmdH:M:S)print(f现在是:{times}by琴棋书画)print()print(欢迎使用笔趣阁小说全本下载工具)print()whileTrue:请求数据keywordinput(请输入你想要搜索的小说名字(0退出):)ifkeyword0:breakseacherurlfhttps:www。biquge9。coms?q{keyword}responesrequests。get(seacherurl,headersheaders)print(respones。text)解析获取数据selectoretree。HTML(respones。text)xsurlsselector。xpath(h4a〔contains(href,book)〕href)获取小说地址列表xsnamesselector。xpath(a〔contains(href,book)〕text())获取小说名字列表xsauthersselector。xpath(p〔contains(class,author)〕text())获取小说作者列表selectorparsel。Selector(respones。text)xsurlsselector。css(。bookinfo。booknamea::attr(href))。getall()xsnamesselector。css(bookinfo。booknamea::text)。getall()xsauthersselector。css(。bookinfop::text)。getall()print(xsauthers,xsnames,xsurls)tbpt。PrettyTable()tb。fieldnames〔序号,书名,作者,小说ID〕num0ifxsnames:print(xsurls,xsnames,xsauthers)xslist〔〕遍历弄表forxsurl,xsname,xsautherinzip(xsurls,xsnames,xsauthers):xsidxsurl。split()〔2〕小说IDxsurlhttps:www。biquge9。comxsurl小说网址xsnamexsname。strip()小说书名xsautherxsauther。split(:)〔1〕小说作者print(xsid,xsurl,xsname,xsauther)dict{书名:xsname,作者:xsauther,ID:xsid}生成字典xslist。append(dict)小说信息列表tb。addrow(〔num,xsname,xsauther,xsid〕)numnum1print(tb)print()print(f一共搜索到{len(xslist)}条数据)print()seadatapd。DataFrame(xslist)print(seadata)输出小说信息列表print()keynuminput(请输入你要下载的小说序号:)print()print(小说正在下载,已完成。。。。。。)xsIDxslist〔int(keynum)〕〔ID〕urlhttps:www。biquge9。combookxsIDurls〔url{}。html。format(str(i))foriinrange(1,5000)〕生成章节地址列表print(urls)forurl1intqdm(urls):遍历章节地址列表data1requests。get(url1,headersheaders)请求章节文本selectorparsel。Selector(data1。text)print(selector)titleselector。css(。contenth1::text)。get()获取章节标题print(title)contentselector。css(chaptercontent::text)。getall()获取章节正文print(content)content1。join(content)print(content1)withopen(xsname。txt,a,encodingutf8)asfile:保存小说内容到TXT文件iftitle!:file。write(title)file。write()file。write(content1)file。write()print(正在保存,title)else:file。write(content1)file。write()else:print(请正确输入小说名字或作者名字,没有查到这本书的数据。。。。。)