【问题标题】:empty results of xpath is not printed不打印 xpath 的空结果
【发布时间】:2019-02-08 09:32:00
【问题描述】:

我也需要从 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】:

    解决办法是

    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 循环也将遍历空结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-31
      • 2019-03-11
      • 1970-01-01
      • 2019-01-07
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多