Scrapy XPath selector multilevel selection error

Source: Internet
Author: User

When extracting Web page content with XPath in learning scrapy, it is sometimes necessary to extract the entire row label content, and then look for the target content from the row label. An error has occurred.

Error code:

def Parse (self, Response):     = Scrapy. Selector (response)    = Sel.xpath ('/html/body/div[2]/div[5]/div[1]/ul/li')      for inch Sel_li:         Print (I.xpath ('//h5/a/text ()'). Extract () [0])

Results:

Big mouth second quarter big mouth big mouth second quarter big mouth second quarter Big mouth second season Big mouth second quarter
...

The extracted content is the next content in the first one.

Post-Revision code:

def Parse (self, Response):     = Scrapy. Selector (response)    = Sel.xpath ('/html/body/div[2]/div[5]/div[1]/ul/li')      for inch Sel_li:         Print (I.xpath ('.//h5/a/text ()'). Extract () [0])

Results:

Big Mouth second quarter new hundred Days Dragon third quarter Danish kinky first quarter bad history seventh quarter shameless nineth quarter ...

In the child XPath, before the path is added . Indicates from the current lookup, after which subsequent content is normally obtained

Other methods:

 def   parse (self, Response): Sel  = Scrapy. Selector (response) Sel_li  = Sel.xpath ( " /    html/body/div[2]/div[5]/div[1]/ul/li   " )  for  i in   Sel_li.extract ():  print  (Scrapy. Selector (text=i). XPath ( '  //h5/a/text ()  " Span style= "COLOR: #800000" > " ). Extract () [0])  print  (scrapy. Selector (text=i). XPath ( '  .//h5/a/text ()   "). Extract () [0]) 

Scrapy XPath selector multilevel selection error

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.