不打印 xpath 的空结果

empty results of xpath is not printed

我也需要从 xml 查询中获取空值,其中 returns 作为结果 array.There 的链接是一些没有链接到 refer.When 的度数打印,则不会打印相应的空结果。

objective正在获取相应度数的链接

我的代码是:

  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  

正在删除空值。 请帮我解决问题

解决方案是

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')

for 循环也将通过空结果进行迭代。