不打印xpath的空结果

问题描述 投票:2回答:1

我需要从xml查询中获取空值,该查询返回链接作为结果数组。有一些度数没有要引用的链接。打印时,相应的空结果不会被打印。

目标是获得相应学位的链接。

我的代码是:

  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  

空值将被删除。请帮我解决这个问题

python xml href
1个回答
1
投票

解决方案是

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循环也将通过空结果迭代。

© www.soinside.com 2019 - 2024. All rights reserved.