{ "info": { "author": "Mikhail Ananyevskiy (aka soomrack)", "author_email": "soomrack@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "Natural Language :: English", "Programming Language :: Python :: 3.4" ], "description": "WebParser\n=========\n\n\nDescription\n-----------\nTiny framework for parsing web.\n\n**Crawler.py** module provides base class for webparser, it can\n\n- open webpages with selenium webdriver;\n- run a set of parsers;\n- handle selenium exceptions;\n- log success and log fail.\n\n\nUsage\n-----\n\nImportant\n'''''''''\n\nTo use a remote webdriver don't forget to start selenium server.\n\n.. code-block:: bash\n\n $ java -jar selenium-server-standalone.jar\n\n\nExample\n'''''''\n\nCreate module contains parsers:\n\n.. code-block:: python\n\n #-------[ amazon.py ]---------------------------------------------------\n from webparser.crawler import Crawler\n\n class AmazonBook(Crawler):\n def __init__(self, url=None):\n self.parsers = {self.parse_title, self.parse_cover_url}\n super().__init__(url)\n\n def parse_title(self):\n title = self.webdriver.find_element_by_xpath(\n \"//span[@id='productTitle'][1]\"\n ).get_attribute('innerHTML')\n self.data['title'] = title\n if title:\n return None\n return 'Title not found.'\n\n def parse_cover_url(self):\n cover_url = self.webdriver.find_element_by_xpath(\n \"//img[@id='imgBlkFront'][1]\"\n ).get_attribute('src')\n self.data['cover_url'] = cover_url\n if cover_url:\n return None\n return 'Cover url not found.'\n #-----------------------------------------------------------------------\n\n\nCreate parsers:\n\n.. code-block:: python\n\n # Example script 01:\n #-------[ parser.py ]---------------------------------------------------\n from webparser.amazon import AmazonBook\n\n amazon_book = AmazonBook('http://...') # Load webpage and parse it\n print(amazon_book.data['title']) # Print parsed book title\n print(amazon_book.data['cover_url']) # Print parsed url title\n #-----------------------------------------------------------------------\n\n # Example script 02:\n #-------[ parser.py ]---------------------------------------------------\n from webparser.amazon import AmazonBook\n\n amazon_book = AmazonBook() # Create object\n amazon_book.get('http://...') # Load webpage\n amazon book.parse_title() # Parse book title\n print(amazon_book.data['title']) # Print parsed book title\n #-----------------------------------------------------------------------\n\n\nGuideline\n---------\n\nWebdriver\n'''''''''\n\nChange default webdriver for new objects\n\n.. code-block:: python\n\n Crawler.webdriver = None\n Crawler.webdriver_default = lambda : Crawler.init_webdriver_chrome_remote(ip, port)\n\nChange default webdriver for new objects of selected class\n\n.. code-block:: python\n\n AmazonBook.webdriver = None\n AmazonBook.webdriver_default = lambda : Crawler.init_webdriver_chrome_remote(ip, port)\n\nChange webdriver for selected object\n\n.. code-block:: python\n\n myobject.webdriver = Crawler.init_webdriver_chrome_remote(ip, port)\n\n\nChild classes\n'''''''''''''\n\nRealization of parsers should be placed in child classes. See the example of child class in the Example section.\n\nChild class should have constructor\n\n.. code-block:: python\n\n def __init__(self, url=None):\n self.parsers = {self.parse_title} # Set of routine parsers\n super().__init__(url) # Parent class constructor\n\nChild class should have parsers\n\n.. code-block:: python\n\n def parse_title(self): # Recommend to begin name with 'parser_'\n '''Parses book title.''' # Docstring is important for logs\n title = self.webdriver.find_element_by_xpath(\n \"//span[@id='productTitle'][1]\"\n ).get_attribute('src') # Recommend to retrieve data with xpath\n self.data['title'] = title # Data should be stored in data[]\n if title:\n return None # If success, return None\n return 'Title not found.' # If failed, return error message\n\nRecommend to make separate class for each webpage type,\nand separate module (with several classes) for each website.\nExample: module \"amazon.py\" with classes \"AmazonBook\", \"AmazonCoupons\".\n\n\nLogging\n'''''''\n\nLevel WARNINGS\n\n* logs fail messages.\n\nLevel INFO\n\n* logs exception messages about fails;\n* logs success messages.\n\nSet log level in your script:\n\n.. code-block:: python\n\n import logging\n logging.basicConfig(level=logging.INFO)\n\n\nCopyright\n---------\nCopyright (c) 2017 Mikhail Ananyevskiy\n\n\nLicense\n-------\n\nThis programm is free software; you can redistribute it and/or modify\nit under the terms of\n\n**MIT License**\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\nTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\nIN THE SOFTWARE.", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/soomrack/webparser", "keywords": "webparser web parser selenium", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "webparser", "package_url": "https://pypi.org/project/webparser/", "platform": "", "project_url": "https://pypi.org/project/webparser/", "project_urls": { "Homepage": "https://github.com/soomrack/webparser" }, "release_url": "https://pypi.org/project/webparser/1.0/", "requires_dist": null, "requires_python": "", "summary": "Tiny framework for parsing web.", "version": "1.0" }, "last_serial": 3273105, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "c997a9f41e298652b425c058090dd5b1", "sha256": "3a2c1fad81a548524d7290037a3fa64906c779f47d479404578ac3e528fd8097" }, "downloads": -1, "filename": "webparser-1.0.tar.gz", "has_sig": false, "md5_digest": "c997a9f41e298652b425c058090dd5b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6494, "upload_time": "2017-10-23T19:36:36", "url": "https://files.pythonhosted.org/packages/fb/c0/fa6c8365b051b9f6914b829c2ec6a7e53cde9359c99ce1a954a280e4dbae/webparser-1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c997a9f41e298652b425c058090dd5b1", "sha256": "3a2c1fad81a548524d7290037a3fa64906c779f47d479404578ac3e528fd8097" }, "downloads": -1, "filename": "webparser-1.0.tar.gz", "has_sig": false, "md5_digest": "c997a9f41e298652b425c058090dd5b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6494, "upload_time": "2017-10-23T19:36:36", "url": "https://files.pythonhosted.org/packages/fb/c0/fa6c8365b051b9f6914b829c2ec6a7e53cde9359c99ce1a954a280e4dbae/webparser-1.0.tar.gz" } ] }