HTML parsing module of Python crawler module

Source: Internet
Author: User
Tags xpath

This is relatively simple, there is nothing to emphasize, if the JSON is returned directly according to the key value, if the page is to use the HTML lxml module XPath parsing.

From lxml import htmlimport jsonclass getnodelist ():    def __init__ (self):        self.getdivxpath= "//div[@class = ' Demo '] "    def use_xpath (self,source):        If Len (source):            root=html.fromstring (source) #html转换成dom对象            Nodelist=root.xpath (Self.getdivxpath) #对dom对象进行xpath解析            If Len (nodelist):                return nodelist            return None                def use_json (self, source,keyname):        If Len (source):            jsonstr=json.loads (source)            value= Jsonstr.get (keyname) #根据具体的键值修改            If Len (value):                return value            return None

  

HTML parsing module of Python crawler module

Related Article

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.