教程 > scrapy 教程 > 阅读:29

scrapy 提取项目——迹忆客-ag捕鱼王app官网

为了从网页中提取数据,scrapy 使用了一种称为基于 xpath 和 css 表达式的选择器的技术。 以下是 xpath 表达式的一些示例

  • /html/head/title - 这将选择 html 文档的 元素内的 </code> 元素。</li> <li><strong>/html/head/title/text()</strong> - 这将选择同一 <code><title></code> 元素中的文本。</li> <li><strong>//td</strong> - 这将从 <code><td></code> 中选择所有元素。</li> <li><strong>//div[@class = "slice"]</strong> - 这将从 div 中选择所有包含属性 class = "slice" 的元素</li> </ul> <p>选择器有四种基本方法如下表所示</p> <table> <thead> <tr> <th align="center">序号</th> <th>方法</th> <th>描述</th> </tr> </thead> <tbody><tr> <td align="center">1</td> <td>extract()</td> <td>它返回一个 unicode 字符串和所选数据。</td> </tr> <tr> <td align="center">2</td> <td>re()</td> <td>它返回一个 unicode 字符串列表,当正则表达式作为参数给出时提取。</td> </tr> <tr> <td align="center">3</td> <td>xpath()</td> <td>它返回一个选择器列表,该列表表示由作为参数给出的 xpath 表达式选择的节点。</td> </tr> <tr> <td align="center">4</td> <td>css()</td> <td>它返回一个选择器列表,代表由作为参数给出的 css 表达式选择的节点。</td> </tr> </tbody></table> <hr> <h2 id="在-shell-中使用选择器">在 shell 中使用选择器</h2> <p>要使用内置的 scrapy shell 演示选择器,我们需要在系统中安装 ipython。 这里重要的是,在运行 scrapy 时,url 应该包含在引号中; 否则带有 <code>&</code> 字符的 url 将不起作用。 我们可以在项目的顶级目录中使用以下命令启动 shell</p> <pre><code class="language-bash">$ scrapy shell "http://www.dmoz.org/computers/programming/languages/python/books/" </code></pre> <p>shell 如下所示</p> <pre><code class="language-bash">[ ... scrapy log here ... ] 2022-01-23 17:11:42-0400 [scrapy] debug: crawled (200) <get http://www.dmoz.org/computers/programming/languages/python/books/>(referer: none) [s] available scrapy objects: [s] crawler <scrapy.crawler.crawler object at 0x3636b50> [s] item {} [s] request <get http://www.dmoz.org/computers/programming/languages/python/books/> [s] response <200 http://www.dmoz.org/computers/programming/languages/python/books/> [s] settings <scrapy.settings.settings object at 0x3fadc50> [s] spider <spider 'default' at 0x3cebf50> [s] useful shortcuts: [s] shelp() shell help (print this help) [s] fetch(req_or_url) fetch request (or url) and update local objects [s] view(response) view response in a browser in [1]: </code></pre> <p><img src="/uploads/221127/i_202211271334265ab703.png" alt="scrapy shell 中使用选择器"></p> <p>当 shell 加载时,我们可以分别使用 <code>response.body</code> 和 <code>response.header</code> 访问正文或标头。 同样,我们可以使用 <code>response.selector.xpath()</code> 或 <code>response.selector.css()</code> 对响应运行查询。</p> <p>例如</p> <pre><code class="language-bash">in [1]: response.xpath('//title') out[1]: [<selector xpath = '//title' data = u'<title>my book - scrapy'>] in [2]: response.xpath('//title').extract() out[2]: [u'<title>my book - scrapy: index: chapters'] in [3]: response.xpath('//title/text()') out[3]: [] in [4]: response.xpath('//title/text()').extract() out[4]: [u'my book - scrapy: index: chapters'] in [5]: response.xpath('//title/text()').re('(\w ):') out[5]: [u'scrapy', u'index', u'chapters']

    提取数据

    要从普通的 html 站点中提取数据,我们必须检查站点的源代码以获取 xpath。 检查后,我们可以看到数据将位于 ul 标记中。 选择 li 标签内的元素。

    以下代码行显示不同类型数据的提取 -

    用于选择 li 标签内的数据

    response.xpath('//ul/li')
    

    用于选择描述

    response.xpath('//ul/li/text()').extract()
    

    用于选择站点标题

    response.xpath('//ul/li/a/text()').extract()
    

    用于选择站点链接

    response.xpath('//ul/li/a/@href').extract()
    

    以下代码演示了上述提取器的使用

    import scrapy
    class myprojectspider(scrapy.spider):
       name = "project"
       allowed_domains = ["dmoz.org"]
       
       start_urls = [
          "http://www.dmoz.org/computers/programming/languages/python/books/",
          "http://www.dmoz.org/computers/programming/languages/python/resources/"
       ]
       def parse(self, response):
          for sel in response.xpath('//ul/li'):
             title = sel.xpath('a/text()').extract()
             link = sel.xpath('a/@href').extract()
             desc = sel.xpath('text()').extract()
             print title, link, desc
    

查看笔记

扫码一下
查看教程更方便
网站地图