Package: webparser
DescriptionTiny framework for parsing web.Crawler.pymodule provides base class for webparser, it canopen webpages with selenium webdriver;run a set of parsers;handle selenium exceptions;log success and log fail.UsageImportantTo use a remote webdriver don’t forget to start selenium server.$java-jarselenium-server-standalone.jarExampleCreate module contains parsers:#-------[ amazon.py ]---------------------------------------------------fromwebparser.crawlerimportCrawlerclassAmazonBook(Crawler):def__init__(self,url=None):self.parsers={self.parse_title,self.parse_cover_url}super().__init__(url)defparse_title(self):title=self.webdriver.find_element_by_xpath("//span[@id='productTitle'][1]").get_attribute('innerHTML')self.data['title']=titleiftitle:returnNonereturn'Title not found.'defparse_cover_url(self):cover_url=self.webdriver.find_element_by_xpath("//img[@id='imgBlkFront'][1]").get_attribute('src')self.data['cover_url']=cover_urlifcover_url:returnNonereturn'Cover url not found.'#-----------------------------------------------------------------------Create parsers:# Example script 01:#-------[ parser.py ]---------------------------------------------------fromwebparser.amazonimportAmazonBookamazon_book=AmazonBook('http://...')# Load webpage and parse itprint(amazon_book.data['title'])# Print parsed book titleprint(amazon_book.data['cover_url'])# Print parsed url title#-----------------------------------------------------------------------# Example script 02:#-------[ parser.py ]---------------------------------------------------fromwebparser.amazonimportAmazonBookamazon_book=AmazonBook()# Create objectamazon_book.get('http://...')# Load webpageamazonbook.parse_title()# Parse book titleprint(amazon_book.data['title'])# Print parsed book title#-----------------------------------------------------------------------GuidelineWebdriverChange default webdriver for new objectsCrawler.webdriver=NoneCrawler.webdriver_default=lambda:Crawler.init_webdriver_chrome_remote(ip,port)Change default webdriver for new objects of selected classAmazonBook.webdriver=NoneAmazonBook.webdriver_default=lambda:Crawler.init_webdriver_chrome_remote(ip,port)Change webdriver for selected objectmyobject.webdriver=Crawler.init_webdriver_chrome_remote(ip,port)Child classesRealization of parsers should be placed in child classes. See the example of child class in the Example section.Child class should have constructordef__init__(self,url=None):self.parsers={self.parse_title}# Set of routine parserssuper().__init__(url)# Parent class constructorChild class should have parsersdefparse_title(self):# Recommend to begin name with 'parser_''''Parses book title.'''# Docstring is important for logstitle=self.webdriver.find_element_by_xpath("//span[@id='productTitle'][1]").get_attribute('src')# Recommend to retrieve data with xpathself.data['title']=title# Data should be stored in data[]iftitle:returnNone# If success, return Nonereturn'Title not found.'# If failed, return error messageRecommend to make separate class for each webpage type,
and separate module (with several classes) for each website.
Example: module “amazon.py” with classes “AmazonBook”, “AmazonCoupons”.LoggingLevel WARNINGSlogs fail messages.Level INFOlogs exception messages about fails;logs success messages.Set log level in your script:importlogginglogging.basicConfig(level=logging.INFO)CopyrightCopyright (c) 2017 Mikhail AnanyevskiyLicenseThis programm is free software; you can redistribute it and/or modify
it under the terms ofMIT LicensePermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.