CNDS博客文章爬取之tkinter

from tkinter import *
import tkinter as tk
import tkinter.messagebox as messagebox
import tkinter.font
import requests
import re
import os

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36'}
def get_onepage(url):
    try:
       response=requests.get(url,headers=headers)
       if response.status_code==200:
           return response.text
       return None
    except:
        print('出错了!!!')
        return None
def save_html(html,filename):
    html=html.encode('utf-8')
    if not os.path.exists('blog'):
        blog_path = os.path.join(os.path.abspath('.'), 'blog')
        os.mkdir(blog_path)
    try:
        fout = open('./blog/' + filename + '.html', 'wb')
        fout.write(html)
    except IOError as e:
        print(e)

def parse_onepage(html):
    pattern = re.compile('<span class="link_view" title="阅读次数"><a href="(.*?)" title="阅读次数">',re.S)
    blog_urls=re.findall(pattern,html)
    for url in blog_urls:
        url2='http://blog.csdn.net/'+url
        html2=get_onepage(url2)
        pattern2=re.compile('<article>.*?<h1 class="csdn_top">(.*?)</h1>',re.S)
        filename=re.findall(pattern2,html2)
        filename = "".join(filename)
        string = re.sub("[\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+",'',filename)
        save_html(html2,string)

def total(html):
    pattern=re.compile('<div id="papelist" class="pagelist">.*?共(.*?)页</span>',re.S)
    total="".join(re.findall(pattern,html))
    # print(total)
    return int(total)


def download():
    blogname = blog_name.get()
    url = 'CSDN博客-IT技术写作分享平台' + blogname
    html=get_onepage(url)
    t=total(html)+1
    for i in range(1,t):
        var2.set('正在抓取第 %d 页'%i)
        url2='http://blog.csdn.net/caimouse/article/list/'+str(i)
        html=get_onepage(url2)
        parse_onepage(html)
    var2.set("******抓取完成********")

root = tk.Tk()
root.title('CNDS博客文章下载器')
root.geometry('300x200')
L1 = tk.Label(root, text='博客名', font=('Courier New', 10, tk.font.BOLD))
L1.grid(row=0, column=0, padx=5, pady=10)
blog_name = tk.Entry(root, font=('Courier New', 10, tk.font.BOLD))
blog_name.grid(row=0, column=1, padx=5, pady=10)
blogname=blog_name.get()


L2 = tk.Label(root, text='线程数', font=('微软雅黑', 10))
L2.grid(row=1, column=0, padx=5, pady=10)
default_value = StringVar()
default_value.set('10')
threadnum = tk.Entry(root, textvariable=default_value, font=('微软雅黑', 10))
threadnum.grid(row=1, column=1, padx=5, pady=10)

button_cancel = tk.Button(root, text='退出', command=root.quit)
button_cancel.grid(row=2, column=0, padx=25, pady=20)

button_cancel = tk.Button(root, text='下载', command=download)
button_cancel.grid(row=2, column=1, padx=5, pady=20)

var2 = StringVar()
L3 = tk.Label(root, font=('微软雅黑', 10), fg='red', textvariable=var2)
L3.grid()
var2.set("准备中...")
root.mainloop()

    原文作者:紫丁香
    原文地址: https://zhuanlan.zhihu.com/p/30949877
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞