python爬虫爬取小说

import requests
from bs4 import BeautifulSoup
from PIL import Image,ImageTk
import tkinter as tk
import io
import pickle
from tkinter import messagebox
index = -1
h = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36’}
r = requests.get(‘https://www.readnovel.com/all’,headers=h)
c = r.content
result_list = []
soup = BeautifulSoup(c,‘html.parser’)
Pagenum = soup.find(‘div’,{‘class’:‘right-book-list’}).find_all(‘li’)[-2].text
for i in range(1,int(10)+1):#循环爬虫
url = ‘https://www.readnovel.com/all?pageSize=10&gender=2&catId=-1&isFinish=-1&isVip=-1&size=-1&updT=-1&orderBy=0&pageNum=’ + str(i)
p_r = requests.get(url,headers=h)
p_c = p_r.content
p_soup = BeautifulSoup(p_c,‘html.parser’)
all_img_list = p_soup.find(‘div’,{‘class’:‘right-book-list’}).find_all(‘li’)
for item in all_img_list:
d = {}
d[‘img’] = item.find(‘div’,{‘class’:‘book-img’}).find(‘img’)[‘src’]#图片
d[‘title’] = item.find(‘div’,{‘class’:‘book-info’}).find(‘h3’).find(‘a’).text#标题
d[‘intro’] = item.find(‘div’,{‘class’:‘book-info’}).find(‘p’,{‘class’:‘intro’}).text#简介

    result_list.append(d)

def showInfo(i):#打开照片,标题,简介
item = result_list[i]
url = ‘http:’ + item[‘img’]
img_r = requests.get(url,headers=h)
img_c = img_r.content
data_stream = io.BytesIO(img_c)
img = Image.open(data_stream)
photo = ImageTk.PhotoImage(img)
imgLable.config(image=photo)
imgLable.image=photo
titleLable.config(text=item[‘title’])
introLabel.config(text=item[‘intro’])

def nextpic():#下一个
global index
index += 1
showInfo(index)

def shagn():#上一个
global index
index -=1
showInfo(index)

#下面是登录界面所用函数
def login():
name = nameValue.get()
password = passwordValue.get()
try:
with open(‘usrInfo.pickle’,‘rb’) as usr_file:
usrInfo = pickle.load(usr_file)
except Exception as e:
with open(‘usrInfo.pickle’,‘wb’) as usr_file:
usrInfo = {‘admin’:‘admin’}
pickle.dump(usrInfo,usr_file)
if name in usrInfo:
if usrInfo[name] == password:
tk.messagebox.showinfo(title=‘欢迎!’,message=’%s 用户欢迎你!’% name)
else:
tk.messagebox.showerror(title=‘错误!’,message=‘密码错误请重试!’)
else:
yn = tk.messagebox.askyesno(title=‘错误!’,message=‘用户不存在,是否继续注册?’)
if yn:
signUp()

def signUp():#登陆界面的
def ok():
name = signUp_nameValue.get()
passwd = signUp_passwordValue.get()
confirmPasswd = confirm_passwordValue.get()

    try:
        with open('usrInfo.packle','rb') as usr_file:
            usrInfo = pickle.load(usr_file)
    except Exception as e:
        with open('usrInfo.pickle','wb') as usr_file:
            usrInfo = {'admin':'admin'}
            pickle.dump(usrInfo,usr_file)
    if name != '':#判断用户名是否为空
        if name in usrInfo:
            tk.messagebox.showinfo(title='提示',message='%s 此用户已经存在'% name)
        else:
            if passwd != '' and passwd == confirmPasswd:
                usrInfo[name] = passwd
                print(usrInfo)
                with open('usrInfo.pickle','wb') as usr_file:
                    pickle.dump(usrInfo,usr_file)
                tk.messagebox.showinfo(title='祝贺您注册成功',message='%s 注册成功!' % name)
                signUp_win.destroy()
            else:
                tk.messagebox.showerror(title='错误',message='密码不能为空或者确认密码')
    else:
        tk.messagebox.showerror(title='错误',message='用户名不能为空')
def cancel():
    signUp_win.destroy()


signUp_win = tk.Toplevel(window)
signUp_win.title('注册')
signUp_win.geometry('500x200')

tk.Label(signUp_win,text='用户名:').place(x=30,y=20)
signUp_nameValue = tk.StringVar()
tk.Entry(signUp_win,textvariable=signUp_nameValue).place(x=100,y=20)

tk.Label(signUp_win,text='密 码:').place(x=30,y=60)
signUp_passwordValue = tk.StringVar()
tk.Entry(signUp_win,textvariable=signUp_passwordValue,show='*').place(x=100,y=60)

tk.Label(signUp_win,text='确认密码;').place(x=30,y=100)
confirm_passwordValue = tk.StringVar()
tk.Entry(signUp_win,textvariable=confirm_passwordValue,show='*').place(x=100,y=100)

tk.Button(signUp_win,text='确定',command=ok,bg='blue').place(x=110,y=140)
tk.Button(signUp_win,text='取消',command=cancel,bg='yellow').place(x=160,y=140)
#登录界面显示框

window = tk.Tk()
window.title(‘登录’)
window.geometry(‘400x300’)

canvas = tk.Canvas(window,width=400,height=120,bg=‘red’)
img = Image.open(‘1.jpg’)#背景照片
img = img.resize((420,320))
photo = ImageTk.PhotoImage(image=img)
canvas.create_image(0,0,anchor=‘nw’,image=photo)
canvas.place(x=0,y=0)

nameLbl = tk.Label(window,text=‘用户名:’)
nameLbl.place(x=30,y=150)

nameValue = tk.StringVar()
tk.Entry(window,textvariable=nameValue).place(x=100,y=150)

tk.Label(window,text=‘密 码:’).place(x=30,y=200)
passwordValue = tk.StringVar()
tk.Entry(window,textvariable=passwordValue,show=’*’).place(x=100,y=200)

tk.Button(window,text=‘登录’,command=login).place(x=120,y=240)
tk.Button(window,text=‘注册’,command=signUp).place(x=180,y=240)

window.mainloop()

#下面是爬虫显示框
window = tk.Tk()

window.title(‘此乃小说免费简介网站’)#网站名
window.geometry(‘500x500’)#框大小

imgLable = tk.Label(window,bg=‘gray’)#显示图片
imgLable.pack()#传上

titleLable = tk.Label(window,bg=‘green’)#标题颜色
titleLable.pack()

introLabel = tk.Label(window,bg=‘red’,height=3)#简介颜色
introLabel.pack()

tk.Button(window,text=‘下一个’,command=nextpic,bg=‘orangered’).place(x=340,y=230)#点击上一个下一个
tk.Button(window,text=‘上一个’,command=shagn,bg=‘yellowgreen’).place(x=130,y=230)
tk.Button(window,text=‘可以开始表演了奥’,bg=‘pink’).place(x=200,y=280)#秀框
#showInfo(index)
window.mainloop()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值