在网络抓取表格时拉回值的问题

时间:2019-04-30 23:31:40

标签: powershell web-scraping

我正在尝试从网页上的表格中提取文本。我使用Invoke-WebRequest拉出网页,将变量设置为显示“ AllElements”,并尝试仅拉出与“ Table”匹配的内部值;但是当我运行脚本时,没有任何回退,也没有显示错误。

$URI = 'https://www.python.org/downloads/release/python-2716/'

$R = Invoke-WebRequest -URI $URI

$R.AllElements|?{$_.Class -eq "table"}|select innerText

我希望在python.org网站上显示表的值,但是当脚本运行时,什么也不会返回。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是因为没有表或表类,它是具有动态生成的有序列表项的div。 您可以在浏览器开发人员工具中使用Edge中的F12或在Firefox,Chrome等操作系统中使用类似的功能来查看此信息...

$URI = 'https://www.python.org/downloads/release/python-2716'

$R = Invoke-WebRequest -URI $URI

$R.AllElements | 
Where {$_.Class -eq 'container' }


$R.AllElements | 
Where {$_.Class -eq 'list-row-container menu' }


($R.AllElements | 
Where {$_.class -eq 'list-row-container menu'}).innerText


($R.AllElements | 
Where {$_.Class -eq 'release-number' })


($R.AllElements | 
Where {$_.Class -eq 'release-number' }).outerHTML


(($R.AllElements | 
Where {$_.Class -eq 'release-number' }).outerHTML -split '<a href="|/">Python')[2]

或者只是这样做...

$R.Links
$R.Links.href
$R.Links.href -match 'downloads'