我也需要从 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 空值正在被删除。请帮我解决问题
1 回答
慕娘9325324
TA贡献1783条经验 获得超4个赞
解决办法是
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 循环也将通过空结果进行迭代。
添加回答
举报
0/150
提交
取消