如何通过PowerShell每次获取动态PHP网页的新内容?
解决方法
问题根源是请求被缓存,服务器返回了旧内容而非重新生成的随机文本,可通过以下两种方式解决:
方法一:禁用请求缓存
在Invoke-WebRequest中添加请求头,强制服务器跳过缓存,返回新生成的内容:
$headers = @{ "Cache-Control" = "no-cache, no-store, must-revalidate" "Pragma" = "no-cache" "Expires" = "0" } $page = Invoke-WebRequest -Uri https://cicero.blazni.cz/lipsum.php -Headers $headers $reading = $page.ParsedHtml.getElementById("rightcol").firstChild.innerHTML $reading
方法二:添加随机查询参数
每次请求时在URL末尾附加随机生成的参数,让服务器识别为全新请求,触发PHP重新生成随机文本:
$rand = Get-Random $page = Invoke-WebRequest -Uri "https://cicero.blazni.cz/lipsum.php?rand=$rand" $reading = $page.ParsedHtml.getElementById("rightcol").firstChild.innerHTML $reading
两种方法都能保证每次运行代码时,服务器都会生成新的随机文本,返回不同结果。
内容的提问来源于stack exchange,提问作者Lucie
相关产品推荐
相关产品推荐

