Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用xpath从格式不正确的页面解析XML_Html_Bash_Xpath_Xmlstarlet_Xmllint - Fatal编程技术网

Html 使用xpath从格式不正确的页面解析XML

Html 使用xpath从格式不正确的页面解析XML,html,bash,xpath,xmlstarlet,xmllint,Html,Bash,Xpath,Xmlstarlet,Xmllint,注意: 在写这个问题时,我注意到有一个Github API可以在不解析HTML的情况下解决我的问题:我决定问它,因为我知道如何解决所描述的解析格式错误的HTML本身的问题。所以请不要投反对票,因为有一个github API!我们可以用任何其他页面抛出验证错误来替换github 我想下载最新版本的。通过获取最新标记的重定向目标,我进入了发布页面 curl $(curl -s "https://github.com/mozilla/geckodriver/releases/latest" --hea

注意: 在写这个问题时,我注意到有一个Github API可以在不解析HTML的情况下解决我的问题:我决定问它,因为我知道如何解决所描述的解析格式错误的HTML本身的问题。所以请不要投反对票,因为有一个github API!我们可以用任何其他页面抛出验证错误来替换github

我想下载最新版本的。通过获取最新标记的重定向目标,我进入了发布页面

curl $(curl -s "https://github.com/mozilla/geckodriver/releases/latest" --head | grep -i location | awk '{print $2}' | sed 's/\r//g') > /tmp/geckodriver.html
带有
geckodriver vx.xxx-linux64.tar.gz
的第一个资产是必需的链接。因为XML是schemantic,所以应该正确地解析它。可以使用。因为xpath对我来说是新的,所以我尝试了一个简单的头部查询。但是
xmllint
抛出了很多错误:

$ xmllint --xpath '//div[@class=Header]' /tmp/geckodriver.html
/tmp/geckodriver.html:51: parser error : Specification mandate value for attribute data-pjax-transient
  <meta name="selected-link" value="repo_releases" data-pjax-transient>
                                                                      ^
/tmp/geckodriver.html:107: parser error : Opening and ending tag mismatch: link line 105 and head
  </head>
         ^
/tmp/geckodriver.html:145: parser error : Entity 'nbsp' not defined
                Sign&nbsp;up
                          ^
/tmp/geckodriver.html:172: parser error : Entity 'rarr' not defined
es <span class="Bump-link-symbol float-right text-normal text-gray-light">&rarr;
...
但结果是相似的

当HTML格式不正确时,是否不可能使用这些工具提取某些数据

curl$(curl-s)https://github.com/mozilla/geckodriver/releases/latest“--head | grep-i location | awk'{print$2}'| sed's/\r//g')>/tmp/geckodriver.html

可能更简单的方法是:

然后,
xmllint
,使用HTML解析器:

xmllint --html --xpath '//div[@class=Header]'
但是,这与该页面上的任何内容都不匹配,因此您可能希望XPath基于以下内容:

'string((//a[span[contains(.,"linux")]])[1]/@href)'
这将产生:

/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux32.tar.gz

因此,
xmllint
xmlstarlet
对格式错误的XML内容抛出错误是完全有效的。我希望得到所选的HTML元素,并且需要一些不打印这些错误的静默模式。
'string((//a[span[contains(.,"linux")]])[1]/@href)'
/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux32.tar.gz