python 制作網站小說下載器
· Python 3.6
· Pycharm
相關模塊使用輸入想看的小說內容,點擊搜索
這里會返回很多結果,我只選擇第一個
網頁數據是靜態數據,但是要搜索,是post請求,需要提價data參數,如下圖所示:
然后通過解析網站數據,獲取第一個小說i的詳情頁url即可
靜態網頁的獲取,難度是不大的。def search(): search_url = ’http://www.xbiquge.la/modules/article/waps.php’ data = {’searchkey’: name } response = requests.post(url=search_url, data=data, headers=headers) selector = get_parsing(response.text) novel_url = selector.css(’.even a::attr(href)’).extract_first()1、獲取每本小說的章節名以及url地址
所有的章節名以及url地址,都包含在dd標簽里面
’/23/23019/11409705.html’ # 這是網頁獲取到的url’http://www.xbiquge.la/23/23019/11409705.html’ # 這是真實的小說章節內容url地址3、小說名字,直接獲取即可。
def download_one_book(index_url): response = get_response(index_url) response.encoding = response.apparent_encoding sel = get_parsing(response.text) book_name = sel.css(’#info h1::text’).get() # 提取了所有章節的下載地址 urls = sel.css(’#list dd a::attr(href)’).getall() # 不要最新的 12 章放在最前main for url in urls:chapter_url = ’http://www.xbiquge.la’ + urlprint(chapter_url)
保存下載每章小說內容
def download_one_chapter(chapter_url, book_name): response = get_response(chapter_url) response.encoding = response.apparent_encoding html = response.text selector = get_parsing(html) h1 = selector.css(’.bookname h1::text’).get() content = selector.css(’#content::text’).getall() lines = [] for c in content:lines.append(c.strip()) print(h1) text = ’n’.join(lines) file = open(book_name + ’.txt’, mode=’a’, encoding=’utf-8’) file.write(h1) file.write(’n’) file.write(text) file.write(’n’) file.close()小說軟件界面
root = Tk()root.title(’小說下載器’)root.geometry(’560x450+400+200’) label = Label(root, text=’請輸入下載小說名字:’, font=(’華文行楷’, 20))label.grid() entry = Entry(root, font=(’隸書’, 20))entry.grid(row=0, column=1) text = Listbox(root, font=(’隸書’, 16), width=50, heigh=15)text.grid(row=2, columnspan=2) button1 = Button(root, text=’開始下載’, font=(’隸書’, 15), command=search)button1.grid(row=3, column=0) button2 = Button(root, text=’退出程序’, font=(’隸書’, 15), command=root.quit)button2.grid(row=3, column=1) root.mainloop()顯示下載內容
def novel_load(title): text.insert(END, ’正在保存:{}’.format(title)) # 文本框滾動 text.see(END) # 更新 text.update()實現效果
以上就是python 制作網站小說下載器的詳細內容,更多關于python 小說下載器的資料請關注好吧啦網其它相關文章!
相關文章:
