Search code examples
pythonxmlhref

empty results of xpath is not printed


I need to get empty values too from xml query which returns the links as the resultant array.There are some degrees which doesn't have links to refer.When printing, the corresponding empty results are not getting printed.

The objective is getting the links of corresponding degrees.

My code is:

  postgraduatedegrees=tree.xpath('//*[@id="block-scholarly- 
  content"]/div/article/div/div/div//*[contains(text(),"Degree 
  of")]/text()')

  postgraduatedegreeslinks=tree.xpath('//*[@id="block-scholarly- 
  content"]/div/article/div/div/div//*[contains(text(),"Degree of")]/@href')

  Output:
   len(postgraduatedegrees)
   Out[222]: 52

  len(postgraduatedegreeslinks)
   Out[223]: 40  

The empty values are getting removed. Please help me to solve the issue


Solution

  • The solution was

    url="the url of the web page"
    page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
    tree = html.fromstring(page.content)
    postgraduate=tree.xpath('//*[@id="block-scholarly-content"]/div/article/div/div/div//*[contains(text(),"Degree of")]')
    for pg in postgraduate:
       pgcourse= pg.xpath('.//text()')
       pglink=pg.xpath('.//@href')
    

    The for loop will iterate via empty results also.