Preface: Sometimes bored to see some funny jokes, embarrassing things Wikipedia is a good site, so you want to use Python to play a bit. Also relatively simple, just write to share. Hey
Environment: Python 2.7 + win7
Now, open the embarrassing encyclopedia website, first to analyze. Address: https://www.qiushibaike.com
Generally like this is the text, look at the source code can see the content.
You can see that it's all in a class content div, so it's easy to match it directly to the regular expression.
<div.*? class="content"> (. *?) </div>
This will then be able to extract the contents of the code, and then look at the paging.
Paging is also very simple, very regular, directly connected to the pages on the line.
OK, now that you're done analyzing it, go straight to the code.
#-*-coding:utf-8-*-#Author:corleone fromTkinterImport*ImportUrllib2,redefLoad (page): URL="http://www.qiushibaike.com/text/page/"+STR (page) +"/?s=4937798"user_agent="mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/53.0.2785.113 safari/537.36"Headers={'user-agent': user_agent} res=urllib2. Request (url,headers =headers) HTML=Urllib2.urlopen (res). Read () Reg=re.compile (R'<div.*?class= "Content" > (. *?) </div>', Re. S) Duanzi=reg.findall (HTML)returnduanzii=0page=1defget ():ifi==0:txtlist=load (page) page+=1ifI<20: Txt.delete (1.0, END) Txt.insert (1.0,txtlist[i].replace ("<span>",""). Replace ("</span>",""). Replace ("</br>",""). Replace ("\ n",""). Replace ("<br/>","")) I+=1GlobalIGlobalpageGlobaltxtlistElse: I=0defMain (): Root=TK ()#Define a windowRoot.title ("Corleone")#Defining window TitlesRoot.geometry ('500x500')#Define window SizeB=button (root,text="Next", width=25,bg="Red", Command=get)#Define a buttonB.pack (Side=bottom)#The layout of the button is placed at the bottom of the window GlobalTXT txt=text (root,font= ("blackbody", 20))#define an editing interfaceTxt.pack (Expand=yes,fill=both)#Edit interface layout automatically changes with window sizeRoot.mainloop ()#make the window appear on the screen all the time.Main ()
Here, Python's own graphical interface library Tkinter is used to do the GUI interface. A page about 20 pieces Next button next read the page.
Hey, so you can see directly. Ok OK This article is also very simple, no technical content, MO offense, the code is I wrote before, and now still can use, sent out:)
Python crawl embarrassing encyclopedia GUI applet