"Reprint" Python2 crawler crawls all HTML files for a path

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 ImportRe3 ImportUrllib24 5  fromCollectionsImportdeque6 7 #to save a file's suffix8suffix='. html'9 #Regular expressions for extracting article titlesTenRex_title=r'<title> (. *?) </title>' One #extract the regular expression of the required link ARex_url=r'/jdbc/(. +?). HTML' - #The seed URL, which starts crawling from this URL -Base_url='http://www.yiibai.com/jdbc/' the  -  - #Save the retrieved text as an HTML file - defsavehtml (file_name,file_content): + #Note The Windows file naming disabled, such as/ -With Open (File_name.replace ('/','_') +suffix,"WB") as F: + #write files with bytes instead of STR, so to transcode A f.write (file_content) at #Get article title - defGetTitle (file_content): -Linkre=Re.search (rex_title,file_content) -     if(Linkre): -         Print('get the article topic:'+linkre.group (1)) -         returnLinkre.group (1) in   - #two data structures, queues and collections used by crawlers toQueue=deque () +Visited=set () - #Initialize seed link the queue.append (Base_url) *Count=0 $  Panax Notoginseng  whileQueue: -Url=queue.popleft ()#team first element out of the team theVisited |= {URL}#Mark as visited +   A     Print('already crawled:'+ STR (count) +'Crawling <---'+URL) theCount + = 1 +urlop=urllib2.urlopen (URL) - #working with HTML links only $     if 'HTML'  not inchUrlop.headers.getheader ('Content-type'): $         Continue - #Avoid program exception abort -     Try: theData=Urlop.read () -Title=gettitle (data). Decode ('Utf-8');Wuyi     #Save File the savehtml (title,data) -     except: Wu         Continue -   About #The regular expression extracts all the links in the page and determines if they have been accessed and then joins the queue to be crawled $Linkre =re.compile (Rex_url) -      forSub_linkinchLinkre.findall (data): -sub_url=base_url+sub_link+SUFFIX; - #has been visited, no longer processed A         ifSub_urlinchvisited: +             Pass the         Else: -         #Set Access $Visited |={Sub_url} the         #Join Queue the queue.append (Sub_url) the             Print('Join the quene--->'+sub_url)

Http://blog.csdn.net/wangshihui512/article/details/51100605#python

22 and 49 lines with the original source if you add the instead can not be executed may be related to the encoding

"Reprint" Python2 crawler crawls all HTML files for a path

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.