Python crawler-Basic process

Source: Internet
Author: User

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

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.