Crawler, the personal understanding is: The use of simulation "Operation Browser" process, automatically get the data we want (or information, than the film AH)
Why learn Reptiles: crawling data for me (equivalent to integrating a class of data)
A. Simple static web crawler architecture:
1.Background Knowledge:url (Uniform Resource Locator, can help us to locate the Web page in the network location, URI is the Uniform Resource Identifier), the HTTP protocol
2. Structure:
A crawler scheduler is required to manage the following programs, such as multithreading management, etc. (for example, blocking time for Web pages can be used to create new applications that are allocated by the operating system)
URL Manager, preventing URLs from being reused, getting URLs, not crawling, and crawling management
3. Workflow:
4.URL Manager Implementation Method:
A. stored in memory (set)
B. Relational database (can be saved permanently)
C. Caching the database (most companies use this approach)
5. Web Downloader:
Save Web pages as HTML, which can be downloaded using urllib and URLLIB2
Implementation method:
A. Simple use of urllib2.open (URL)
B. Add the request method, send the header, disguised as a browser
C. Adding a Cookiejar cookie container
1 #Coding=utf-82 ImportUrllib23 ImportCookielib4URL ="http://www.baidu.com"5 Print 'Method 1'6 #Please make sure the URL is legitimate7Response1 =urllib2.urlopen (URL)8 ifResponse1.getcode () ==200:9 Print 'Read Web page Success'Ten Print 'Length:', One PrintLen (Response1.read ()) A Else: - Print 'failed to read web page' - the Print 'METHOD2:' -Request =Urllib2. Request (URL) -Request.add_header ("usr_agent","mozilla/6.0") -Response2 =Urllib2.urlopen (Request) + ifResponse2.getcode () ==200: - Print 'Read Web page Success' + Print 'Length:', A PrintLen (Response2.read ()) at Else: - Print 'failed to read web page' - - Print 'Method3:' -CJ =Cookielib. Cookiejar () -Opener =Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ)) in Urllib2.install_opener (opener) -RESPONSE3 =urllib2.urlopen (URL) to ifResponse3.getcode () ==200: + Print 'Read Web page Success' - Print 'Length:', the PrintLen (Response3.read ()) * PrintCJ $ PrintResponse3.read ()Panax Notoginseng Else: - Print 'failed to read web page'
View Code
6. Web parser:
To download good HTML as a string, find out
1. Regular expression Matching
2.html.parser
3.lxml Parser
4.BeautifulSoup
With the DOM (Document Object Model) structured parsing, the following is its syntax
1 #Coding=utf-82 ImportRe3 4 fromBs4ImportBeautifulSoup5Html_doc ="""6 7 <body>8 <p class= "title" ><b>the dormouse ' s story</b></p>9 Ten <p class= "story" >once upon a time there were three Little Sisters; and their names were One <a href= "Http://example.com/elsie" class= "Sister " id= "Link1" >ELSIE</A> A <a href= "http://example.com/lacied" class= "sister" id= "Link2" >Lacie</a> and - <a href= "Http://example.com/tillie" class= "Sister " id= "Link3" >Tillie</a>; - and they lived at the bottom of a well.</p> the - <p class= "story" >...</p> - """ - #Create +Ccssoup = BeautifulSoup (Html_doc,'Html.parser', from_encoding='UTF8') - #Get all links +Links= Ccssoup.find_all ('a') A forLinkinchLinks: at Printlink.name,link['href'],link.get_text () - PrintCCSSOUP.P ('class') - - Print 'regular Match' -Link_node = Ccssoup.find ('a', href= Re.compile (r"h"), class_='Sister') - PrintLink_node inLink_node = Ccssoup.find ('a', href= Re.compile (r"D")) - PrintLink_node
5. Scheduler
Reference:
http://www.imooc.com/video/10686
Https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
Regular Expressions:
Http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
Pycharm: Using Tutorials
http://blog.csdn.net/pipisorry/article/details/39909057
Python2 Crawler Beginner Notes