Python written by web spider (web crawler)

Source: Internet
Author: User

Python-written web spider:

<span style= "FONT-SIZE:14PX;" ># Web spider# author Vince 2015/7/29import urllib2import re# get href Contentpattern = ' <a (?: \ \s+.+?) *?\\s+href=\ "([h]{1}[^\"]*?) \ "' t = Set (" ") # Collection of Urldef fecth (URL): http_request = Urllib2.  Request (URL) http_request.add_header (' user-agent ', ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/43.0.2357.134 safari/537.36 ') Http_response = Urllib2.urlopen (http_request) print Http_response.code if H            Ttp_response.code = = 200:for I in range (0,2000): # $ rows html = Http_response.readline ()                     If html = = ": Break else:a = Re.search (pattern, HTML) if a: For href in a.groups (): Print href t.add (href) # main STA    rt#if __name__ = = ' __main__ ': url = ' http://blog.csdn.net/' # target sitet.clear () t.add (URL) while (len (t)! = 0): UU = T.pop () print UU fecth (UU) </span> 

If you do not set user-agent, some websites will not allow access, the newspaper 403

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python written by web spider (web crawler)

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.