0

我需要抓取一个 xml 页面http://www.10why.net/sitemap.xml 这只是我想要的 url 表

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
import re

thename = "sitemap"

class ReviewSpider(BaseSpider):
    name = thename
    allowed_domains = ['10why.net']
    start_urls = ['http://www.10why.net/sitemap.xml']

    def parse(self, response):
        hxs = HtmlXPathSelector(response)

        content = hxs.select('//table[@cellpadding="5"]/tbody//a')

        print content
        for c in content:


            file = open('%s.txt' % thename, 'a')
            file.write("\n")
            file.write(c)
            file.close()

打印的内容是 [] (空列表)我用来能够在普通的 html 页面而不是站点地图 xml 页面上爬取东西。请帮我。PS:我自己写的文件有其他原因。

4

1 回答 1

1

我猜这是因为您正在查看浏览器用来显示XML 的 HTML,而不是来自服务器的原始 XML。当我查看给定的 URL 时,我看到一个类似于以下内容的 XML 结构:

<urlset>
   <url>
      <loc>http://www.10why.net/20130321/bb-nuan/</loc>
      <lastmod>2013-03-21T01:51:31+00:00</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.2</priority>
   </url>
</urlset>

您可能希望使用更类似于以下的 XPath 表达式:

//urlset/url/loc

获取站点地图中的所有 URL。

于 2013-08-08T01:58:04.303 回答