<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Scrapy at a glance > 譯者:[OSGeo 中國](https://www.osgeo.cn/) Scrapy是一個應用程序框架,用于對網站進行爬行和提取結構化數據,這些結構化數據可用于各種有用的應用程序,如數據挖掘、信息處理或歷史存檔。 盡管Scrapy最初是為 [web scraping](https://en.wikipedia.org/wiki/Web_scraping) 它還可以用于使用API提取數據(例如 [Amazon Associates Web Services](https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html) )或者作為一個通用的網絡爬蟲。 ## 瀏覽示例 Spider 為了向您展示Scrapy給桌子帶來了什么,我們將用最簡單的方法來運行一個Scrapy Spider 的例子。 以下是一個 Spider 代碼,它從網站http://quotes.toscrape.com上抓取著名的引語,按照以下頁碼: ```py import scrapy class QuotesSpider(scrapy.Spider): name = 'quotes' start_urls = [ 'http://quotes.toscrape.com/tag/humor/', ] def parse(self, response): for quote in response.css('div.quote'): yield { 'text': quote.css('span.text::text').get(), 'author': quote.xpath('span/small/text()').get(), } next_page = response.css('li.next a::attr("href")').get() if next_page is not None: yield response.follow(next_page, self.parse) ``` 把它放在一個文本文件中,命名為 `quotes_spider.py` 然后用 [`runspider`](../topics/commands.html#std:command-runspider) 命令: ```py scrapy runspider quotes_spider.py -o quotes.json ``` 完成后,您將 `quotes.json` 以JSON格式提交一個引號列表,其中包含文本和作者,如下所示(此處重新格式化以提高可讀性): ```py [{ "author": "Jane Austen", "text": "\u201cThe person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.\u201d" }, { "author": "Groucho Marx", "text": "\u201cOutside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.\u201d" }, { "author": "Steve Martin", "text": "\u201cA day without sunshine is like, you know, night.\u201d" }, ...] ``` ### 剛剛發生了什么? 當你運行命令時 `scrapy runspider quotes_spider.py` 斯克里奇在里面尋找 Spider 的定義,然后用它的爬行引擎運行。 通過向中定義的URL發出請求啟動的爬網 `start_urls` 屬性(在本例中,只有引號的URL _humor_ 并調用默認回調方法 `parse` ,將響應對象作為參數傳遞。在 `parse` 回調,我們使用CSS選擇器循環引用元素,生成一個包含提取的引號文本和作者的python dict,查找到下一頁的鏈接,并使用它調度另一個請求。 `parse` 方法作為回調。 在這里,您注意到Scrapy的一個主要優點:請求是 [scheduled and processed asynchronously](../topics/architecture.html#topics-architecture) . 這意味著Scrapy不需要等待請求完成和處理,它可以同時發送另一個請求或做其他事情。這也意味著,即使某些請求失敗或在處理過程中發生錯誤,其他請求也可以繼續進行。 雖然這使您能夠非常快速地進行爬行(同時以容錯的方式發送多個并發請求),但Scrapy還使您能夠控制爬行的禮貌性。 [a few settings](../topics/settings.html#topics-settings-ref) . 您可以在每個請求之間設置下載延遲、限制每個域或每個IP的并發請求量,甚至 [using an auto-throttling extension](../topics/autothrottle.html#topics-autothrottle) 它試圖自動解決這些問題。 注解 這是使用 [feed exports](../topics/feed-exports.html#topics-feed-exports) 要生成JSON文件,您可以輕松地更改導出格式(例如XML或CSV)或存儲后端(FTP或 [Amazon S3](https://aws.amazon.com/s3/) 例如)。你也可以寫一個 [item pipeline](../topics/item-pipeline.html#topics-item-pipeline) 將項目存儲在數據庫中。 ## 還有什么? 你已經看到了如何使用Scrapy從網站中提取和存儲項目,但這只是表面現象。Scrapy提供了許多強大的功能,使抓取變得簡單和高效,例如: * 內置支持 [selecting and extracting](../topics/selectors.html#topics-selectors) 使用擴展的CSS選擇器和XPath表達式從HTML/XML源中獲取數據,并使用正則表達式提取助手方法。 * 安 [interactive shell console](../topics/shell.html#topics-shell) (ipython-aware)用于嘗試使用css和xpath表達式來獲取數據,在編寫或調試spider時非常有用。 * 內置支持 [generating feed exports](../topics/feed-exports.html#topics-feed-exports) 以多種格式(json、csv、xml)存儲在多個后端(ftp、s3、本地文件系統) * 強大的編碼支持和自動檢測,用于處理外部、非標準和中斷的編碼聲明。 * [Strong extensibility support](../index.html#extending-scrapy) ,允許您使用 [signals](../topics/signals.html#topics-signals) 以及定義良好的API(中間件, [extensions](../topics/extensions.html#topics-extensions) 和 [pipelines](../topics/item-pipeline.html#topics-item-pipeline) ) * 廣泛的內置擴展和用于處理的中間產品: * cookie和會話處理 * HTTP功能,如壓縮、身份驗證、緩存 * 用戶代理欺騙 * robots.txt * 爬行深度限制 * 更多 * A [Telnet console](../topics/telnetconsole.html#topics-telnetconsole) 用于掛接到運行在Scrapy進程中的Python控制臺,以便內省和調試爬蟲程序 * 還有其他的好東西,比如可重復使用的 Spider [Sitemaps](https://www.sitemaps.org/index.html) 和XML/CSV源,這是 [automatically downloading images](../topics/media-pipeline.html#topics-media-pipeline) (或任何其他媒體)與抓取的項目、緩存DNS解析程序等相關! ## 下一步是什么? 接下來的步驟是 [install Scrapy](install.html#intro-install) , [follow through the tutorial](tutorial.html#intro-tutorial) 學習如何創建一個完整的 Scrapy 項目和 [join the community](https://scrapy.org/community/) . 感謝您的關注!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看