Oneself is engaged in the automobile industry, so first to do the first procedure is to crawl Sohu Car Sales database (http://db.auto.sohu.com/cxdata/);
The database provides monthly sales for cars from 07 to the present, with one XML data for each model, such as flash Sales: Http://db.auto.sohu.com/xml/sales/model/model1004sales.xml
What needs to be done now is to traverse all models to save ' model----Date----Sales ' in this format.
#!/usr/bin/python#-*-coding:utf-8-*-ImportUrllib2,string,re,timej=0file= Open ('D:\Program files\notepad++portable\app\notepad++\databasesohu.txt','R'). Read () F=file.split ('\ n') forNinchRange (0,len (f)):#Start Access ifF[n]<>"": J=j+1WB=urllib2.urlopen ('Http://db.auto.sohu.com/xml/sales/model/model'+str (F[n]) +'Sales.xml'). Read ()#Get car nameCode=wb[wb.index ('name=') +6:wb.index ('">')] Model=f[n]+"---"+Code#Print Model #标记用的reg='sales date=. (.*?). Salesnum=. (.*?). />' #Regular Expressionslist=Re.compile (reg). FindAll (WB) forIinchRange (len (list), 0,-1): Lt=list[i-1] LT=lt[0]+"---"+lt[1] Mdata=model+"---"+LTPrintmdata file1= Open ('D:\Program files\notepad++portable\app\notepad++\save.txt','a') File1.write (Mdata+'\ n') File1.close ()#Time DelayTime.sleep (0.5) Else: Print ' Over'PrintJ
File = Open (' D:\Program files\notepad++portable\app\notepad++\databasesohu.txt ', ' R '). Read () f=file.split (' \ n ') )
Open the Model Code encyclopedia and split with newline characters.
Wb=urllib2.urlopen (' Http://db.auto.sohu.com/xml/sales/model/model ' +str (f[n]) + ' Sales.xml '). Read ()
Then we start to traverse the car, access it with URLLIB2 and get the name model.
Get dates and sales with regular expressions (also available in XML processing here).
Saves the data to the text document.
The question for beginners to note is the method of reading the file in Python, where the open (, ' a ') is used, which is the meaning of Add.
Reference: http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001374738281887b88350bd21544e6095d55eaf54cac23f000
Python Python introduction learning web crawler Sohu Car Database