使用Powershell从Xml Sitemaps下载URL

时间:2014-08-23 06:56:40

标签: powershell caching powershell-v2.0

我在几个不同的网站上有一个标准的XML站点地图。 http://example.com/sitemaphttp://otherexample.com等......

采用标准格式

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">

    <url>
        <loc>http://www.example.co.uk</loc>
        <lastmod>2014-07-08T08:28:26+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.3</priority>
    </url>


    <url>
        <loc>http://www.example.co.uk/page-name</loc>
        <lastmod>2013-02-05T13:36:02+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.7</priority>
    </url>

     etc....

</urlset>

我希望能够提供站点地图的网址列表。并将powershell文件分别发送到每个站点。

获取每个站点的站点地图xml,然后从站点地图文件中单独下载所有URL。基本上向每个网址发出请求(我不想存储下载内容)。

这个想法是在网站更新后会触发每个网站的所有主页面的缓存,所以当用户访问网站时,他们会有一个缓存版本。

关于我如何解决这个问题的任何想法?我开始尝试使用Wget但在Win8 / Server上遇到了问题。所以认为Powershell可能是更好的选择。

1 个答案:

答案 0 :(得分:1)

如果您可以运行powerhell V3,这是一种方式: iwrinvoke-webrequest

的别名
$maps=@("http://server.com/sitemap.xml","http://server2.com/sitemap.xml")
$maps |%{
    [xml]$response=iwr $_ |select -expand content
    #get all urls in the sitemap
    $response.urlset.url |%{
        #make a get request on each url
        echo "hitting : " $_.loc
        iwr $_.loc |out-null

    }
}

如果你不能切换到V3,你将不得不使用.net方法,用以下模式替换iwr:

$client=New-Object system.Net.WebClient;
[xml]$response=$client.DownloadString("http://server.com/sitemap.xml")