python爬虫搜索文献_python爬虫进阶——添加搜索功能

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

import urllib.request

import os

import time

from urllib.parse import quote

search_input = input("请输入搜索的书名或作者:")

search_Book = "https://www.qidian.com/search?kw=" + quote(search_input)

page = urllib.request.urlopen(search_Book).read()

txt = page.decode('utf-8')

def bookname():

filter_bookname = r'

(.+?)
'

book_name_nosub = re.findall(filter_bookname, txt, re.S)

book_name_sub = re.sub(r'||', '', " ".join(book_name_nosub))

book_name = book_name_sub.split(' ') # 书名

return book_name

def bookChapter():

filter_Chapter = r'

list2 = [" "] * 10

number=["⓪","⓵","⓶","⓷","⓸","⓹","⓺","⓻","⓼","⓽"]

a = list(map(lambda c,x, a, y, b, z,: c+x + a + y + b + z, number,bookname(), list1, bookChapter(), list2, state()))

b ="\n".join(a)

return b

print(booklist())

i=int(input("输入你要下载书的号数:"))

def book_link(i):

filter_book_id = r'

'

bookname = re.findall(filter_bookname, page, re.S)

filter_chaptername = r'

(.+?)

' #

第4章 继任者

chaptername = re.findall(filter_chaptername, page, re.S) # 获取章节和章节名字

i = 0

for txt in html:

line = html[i]

f = open(img_path + chaptername[0] + ".txt", "a") # a代表追加模式,不覆盖

f.write(line + "\n")

f.close()

i = i + 1

print(chaptername[0] + " 下载完成")

next = r'下一章

nextread = re.findall(next, page, re.S)

b = ''

link = "https://" + b.join(nextread) # 本页的下一章链接

z = z + 1

img_path2 = "E:/txt/" + bookname[0]

c = 0

if not os.path.exists(img_path2): # 如果文件不存在,则重命名文件

os.rename(img_path, img_path2) # 文件夹重命名

print("下载完成")

else:

path = "E:/txt/txt/"

downloadtime = time.strftime("%Y%m%d%I%M%S", time.localtime())

os.rename(img_path, img_path2 + downloadtime)

print("文件名:" + bookname[0] + " 已存在,重命名为:" + bookname[0] + downloadtime + "\n" + "请勿重复操作")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值