Objective
This article is to introduce the use of Python crawling mobile phone ownership information, the article gives a detailed sample code, I believe that the understanding and learning of everyone is helpful, the following for the Python code, relatively simple for reference.
Sample code
#-*-coding:utf-8-*-import requests,re o = open (' Data.txt ', ' a ') e = open (' Error.txt ', ' a ') BaseURL = ' Http://www.iluohe . com/' r = Requests.get (' http://www.iluohe.com/all.shtml ',) links = re.findall (' <a href= ' (city/.*?/.*?) ' Target ', R.content.decode ("GBK"). Encode ("Utf-8")) for link in links:link = baseurl+link citydata = requests.get (link) if Cityda Ta.status_code >= 300:e.writelines (link+ "\ n") Else:citydata = CityData.content.decode ("GBK"). Encode ("Utf-8") pro Vincetemp = Re.findall (' <div class= "Nameszu" ><a href= ". *?" > (. *?) </a></div> ', citydata) if provincetemp:province = provincetemp[0] City = Re.findall (' <meta name= ' descr Iption "Content=" (. *?) Total ', citydata] [0] tempdata = Re.findall (' <div class= ' ab_menu.*?</span> (. *?) \ .*?</div>.*?<ul> ( .*?) </ul> ', citydata) for temp in tempdata:carrier = temp[0] numbers = Re.findall (' "> (. *?) </a></li> ', temp[1]) for number in Numbers:text = number +"," + Carrier + "," + City + "," + Province O.writelines (text) o.writelines (' \ n ') else:e.writelines (link+ "\ n") o . Close () print "over!"
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.