Python Python introduction learning web crawler Sohu Car Database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.