Python初识 python爬虫-简单方式爬取藏头诗

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>', 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*(.*?)
', 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()

你可能感兴趣的:(Python)