Python初識 python爬蟲-簡單方式爬取藏頭詩

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/Q718330882/article/details/80046355
from tkinter import *
import requests
import re
from tkinter import messagebox

def search( ):
    resUrl = 'http://www.shicimingju.com/cangtoushi/index.html'
    params = {
        'kw':entry.get(),
        'position':0,
        'zishu':7,
    }
    req = requests.post( resUrl, params = params )
    req.encoding = 'utf-8'
    patten = re.compile( r'<div.*?class="cangtoushi-item">(.*?)<\/div>', re.S|re.I|re.M )
    pregMatch = patten.findall( req.text )

    with open('C:\\Users\Administrator\Desktop\poem.txt', 'w') as f:
        i = 0;
        for poem in pregMatch:
            i += 1
            strPoem = re.findall( '\s*(.*?)<br>', poem )
            f.write( '第{}首詩:\n' . format( i ) )
            for str in strPoem:
                f.write( str + '\n' )
            f.write( '\n\n' )
        messagebox.showinfo( '提示信息', '已寫入文本數據,抓取完成' )
        f.close()


window = Tk()

window.title( 'Python 應用程序' )
window.geometry( '600x500+700+300' )

entry = Entry( window )
entry.grid()

button = Button( window, text = '生成藏頭詩', command = search )
button.grid( row = 0, column = 1 )



window.mainloop()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章