Crawling Web content with Python2 and Python3 masquerading browsers, respectively

Source: Internet
Author: User
Python Web crawling is very powerful, and using Urllib or URLLIB2 can easily crawl Web content. But most of the time we have to be aware that many sites are set up with anti-collection features, not so easy to grab the content you want.

Today I will share the download Python2 and python3 How to simulate the browser to skip the mask to crawl.

The most basic crawl:

#! /usr/bin/env python#-*-coding=utf-8-*-# @Author pythontabimport urllib.requesturl = "http://www.pythontab.com" html = u Rllib.request.urlopen (URL). Read () print (HTML)

But... Some sites can not be crawled, the anti-collection settings, so we have to change the method

Python2 (latest stable version python2.7)

#! /usr/bin/env python#-*-coding=utf-8-*-# @Author pythontab.comimport urllib2url= "http://pythontab.com" Req_header = {' User-agent ': ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 safari/537.11 ', '             Accept ': ' text/html;q=0.9,*/*;q=0.8 ',             ' accept-charset ': ' iso-8859-1,utf-8;q=0.7,*;q=0.3 ',             ' Accept-encoding ': ' gzip ',             ' Connection ': ' Close ',             ' Referer ': None #注意如果依然不能抓取的话, here you can set the host to crawl the site             }req_ Timeout = 5req = Urllib2. Request (url,none,req_header) resp = Urllib2.urlopen (req,none,req_timeout) HTML = Resp.read () print (HTML)

Python3 (latest stable version python3.3)

#! /usr/bin/env python#-*-coding=utf-8-*-# @Author pythontabimport urllib.request  url = "Http://www.pythontab.com" headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 SAFARI/5 37.11 ',             ' Accept ': ' text/html;q=0.9,*/*;q=0.8 ',             ' accept-charset ': ' iso-8859-1,utf-8;q=0.7,*;q=0.3 ',             ' Accept-encoding ': ' gzip ',             ' Connection ': ' Close ',             ' Referer ': None #注意如果依然不能抓取, here you can set crawl site host             }  Opener = Urllib.request.build_opener () opener.addheaders = [headers]data = Opener.open (URL). Read () print (data)
  • Related Article

    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.