Based on the implementation of Python3, get the basic Technology of Web page: request, Urllib, selenium; The basic technology of Parsing Web page: Re regular expression, BeautifulSoup, and lxml; storage technology: Database or table.
Python Environment setup:
1, Pycharm;
2, Anaconda, suitable for basic introduction, with many packages, there is no need to install.
3, Jupyter notebook.
The blog post environment is then based on the Anaconda Spyder or Pycharm.
Crawl simple Web pages with Urllib
Import"http://www.douban.com/" Request= Urllib.request.Request (URL) # request # crawl result data=response.read () data =data.decode ('utf-8') # set decoding mode print (data) # Print Results
Since many sites have some anti-crawler settings, we simulate the browser login page to download the crawled data to a local
headers = {' user-agent ':' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) 'chrome/51.0.2704.63 safari/537.36 '}
Download to local, function:
def saveFile (data): " c:\\users\\leon\\desktop\\02_douban.html " = Open (path,'wb') f.write (data) f.close ()
The overall code is as follows:
defsaveFile (data): Path="c:\\users\\leon\\desktop\\02_douban.html"F= Open (Path,'WB') f.write (data) f.close ()ImportUrllib.requesturl="http://www.douban.com/"Headers= {'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko)' 'chrome/51.0.2704.63 safari/537.36'}request= Urllib.request.Request (url=url,headers=headers) Response=Urllib.request.urlopen (Request) Data=response.read () data=data.decode ('Utf-8')Print(data) saveFile (data)
Python crawler-Basic process