最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

scrapy spider的幾種爬取方式實(shí)例代碼

 更新時間:2018年01月25日 14:14:48   作者:NodYoung  
這篇文章主要介紹了scrapy spider的幾種爬取方式實(shí)例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本節(jié)課介紹了scrapy的爬蟲框架,重點(diǎn)說了scrapy組件spider。

spider的幾種爬取方式:

  1. 爬取1頁內(nèi)容
  2. 按照給定列表拼出鏈接爬取多頁
  3. 找到‘下一頁'標(biāo)簽進(jìn)行爬取
  4. 進(jìn)入鏈接,按照鏈接進(jìn)行爬取

下面分別給出了示例

1.爬取1頁內(nèi)容

#by 寒小陽(hanxiaoyang.ml@gmail.com)

import scrapy


class JulyeduSpider(scrapy.Spider):
  name = "julyedu"
  start_urls = [
    'https://www.julyedu.com/category/index',
  ]

  def parse(self, response):
    for julyedu_class in response.xpath('//div[@class="course_info_box"]'):
      print julyedu_class.xpath('a/h4/text()').extract_first()
      print julyedu_class.xpath('a/p[@class="course-info-tip"][1]/text()').extract_first()
      print julyedu_class.xpath('a/p[@class="course-info-tip"][2]/text()').extract_first()
      print response.urljoin(julyedu_class.xpath('a/img[1]/@src').extract_first())
      print "\n"

      yield {
        'title':julyedu_class.xpath('a/h4/text()').extract_first(),
        'desc': julyedu_class.xpath('a/p[@class="course-info-tip"][1]/text()').extract_first(),
        'time': julyedu_class.xpath('a/p[@class="course-info-tip"][2]/text()').extract_first(),
        'img_url': response.urljoin(julyedu_class.xpath('a/img[1]/@src').extract_first())
      }

2.按照給定列表拼出鏈接爬取多頁

#by 寒小陽(hanxiaoyang.ml@gmail.com)

import scrapy


class CnBlogSpider(scrapy.Spider):
  name = "cnblogs"
  allowed_domains = ["cnblogs.com"]
  start_urls = [
    'http://www.cnblogs.com/pick/#p%s' % p for p in xrange(1, 11)
    ]

  def parse(self, response):
    for article in response.xpath('//div[@class="post_item"]'):
      print article.xpath('div[@class="post_item_body"]/h3/a/text()').extract_first().strip()
      print response.urljoin(article.xpath('div[@class="post_item_body"]/h3/a/@href').extract_first()).strip()
      print article.xpath('div[@class="post_item_body"]/p/text()').extract_first().strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/a/text()').extract_first().strip()
      print response.urljoin(article.xpath('div[@class="post_item_body"]/div/a/@href').extract_first()).strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_comment"]/a/text()').extract_first().strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_view"]/a/text()').extract_first().strip()
      print ""

      yield {
        'title': article.xpath('div[@class="post_item_body"]/h3/a/text()').extract_first().strip(),
        'link': response.urljoin(article.xpath('div[@class="post_item_body"]/h3/a/@href').extract_first()).strip(),
        'summary': article.xpath('div[@class="post_item_body"]/p/text()').extract_first().strip(),
        'author': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/a/text()').extract_first().strip(),
        'author_link': response.urljoin(article.xpath('div[@class="post_item_body"]/div/a/@href').extract_first()).strip(),
        'comment': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_comment"]/a/text()').extract_first().strip(),
        'view': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_view"]/a/text()').extract_first().strip(),
      }

3.找到‘下一頁'標(biāo)簽進(jìn)行爬取

import scrapy
class QuotesSpider(scrapy.Spider):
  name = "quotes"
  start_urls = [
    'http://quotes.toscrape.com/tag/humor/',
  ]

  def parse(self, response):
    for quote in response.xpath('//div[@class="quote"]'):
      yield {
        'text': quote.xpath('span[@class="text"]/text()').extract_first(),
        'author': quote.xpath('span/small[@class="author"]/text()').extract_first(),
      }

    next_page = response.xpath('//li[@class="next"]/@herf').extract_first()
    if next_page is not None:
      next_page = response.urljoin(next_page)
      yield scrapy.Request(next_page, callback=self.parse)

4.進(jìn)入鏈接,按照鏈接進(jìn)行爬取

#by 寒小陽(hanxiaoyang.ml@gmail.com)

import scrapy


class QQNewsSpider(scrapy.Spider):
  name = 'qqnews'
  start_urls = ['http://news.qq.com/society_index.shtml']

  def parse(self, response):
    for href in response.xpath('//*[@id="news"]/div/div/div/div/em/a/@href'):
      full_url = response.urljoin(href.extract())
      yield scrapy.Request(full_url, callback=self.parse_question)

  def parse_question(self, response):
    print response.xpath('//div[@class="qq_article"]/div/h1/text()').extract_first()
    print response.xpath('//span[@class="a_time"]/text()').extract_first()
    print response.xpath('//span[@class="a_catalog"]/a/text()').extract_first()
    print "\n".join(response.xpath('//div[@id="Cnt-Main-Article-QQ"]/p[@class="text"]/text()').extract())
    print ""
    yield {
      'title': response.xpath('//div[@class="qq_article"]/div/h1/text()').extract_first(),
      'content': "\n".join(response.xpath('//div[@id="Cnt-Main-Article-QQ"]/p[@class="text"]/text()').extract()),
      'time': response.xpath('//span[@class="a_time"]/text()').extract_first(),
      'cate': response.xpath('//span[@class="a_catalog"]/a/text()').extract_first(),
    }

總結(jié)

以上就是本文關(guān)于scrapy spider的幾種爬取方式實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • python調(diào)用并鏈接MATLAB腳本詳解

    python調(diào)用并鏈接MATLAB腳本詳解

    這篇文章主要介紹了python調(diào)用并鏈接MATLAB腳本詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • python curl2pyreqs 生成接口腳本實(shí)戰(zhàn)教程

    python curl2pyreqs 生成接口腳本實(shí)戰(zhàn)教程

    這篇文章主要介紹了python curl2pyreqs 生成接口腳本實(shí)戰(zhàn)教程,首先下載 curl2pyreqs 庫,打開調(diào)試模式,在Network這里獲取接口的cURL,需要的朋友可以參考下
    2023-10-10
  • Python matplotlib 繪制雙Y軸曲線圖的示例代碼

    Python matplotlib 繪制雙Y軸曲線圖的示例代碼

    Matplotlib是非常強(qiáng)大的python畫圖工具,這篇文章主要介紹了Python matplotlib 繪制雙Y軸曲線圖,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 利用Pygame制作躲避僵尸游戲

    利用Pygame制作躲避僵尸游戲

    本文參考了神廟逃亡,利用Pygame實(shí)現(xiàn)一個人躲避僵尸的小游戲,主要的是精靈、精靈組之間相撞、相交的處理,感興趣的可以了解一下
    2022-05-05
  • 對pytorch中不定長序列補(bǔ)齊的操作

    對pytorch中不定長序列補(bǔ)齊的操作

    這篇文章主要介紹了對pytorch中不定長序列補(bǔ)齊的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python中的decorator的作用詳解

    python中的decorator的作用詳解

    這篇文章主要介紹了python中的decorator的作用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Spectral?clustering譜聚類算法的實(shí)現(xiàn)代碼

    Spectral?clustering譜聚類算法的實(shí)現(xiàn)代碼

    譜聚類是從圖論中演化出來的算法,它的主要思想是把所有的數(shù)據(jù)看做空間中的點(diǎn),這些點(diǎn)之間可以用邊連接起來,今天通過本文給大家介紹Spectral?clustering譜聚類算法的實(shí)現(xiàn),感興趣的朋友一起看看吧
    2022-04-04
  • python監(jiān)控鍵盤輸入實(shí)例代碼

    python監(jiān)控鍵盤輸入實(shí)例代碼

    這篇文章主要介紹了python監(jiān)控鍵盤輸入實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 一篇文章帶你了解幾個好用的Python技巧

    一篇文章帶你了解幾個好用的Python技巧

    這篇文章主要介紹了幾個Python小技巧,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python中l(wèi)ogger日志模塊詳解

    Python中l(wèi)ogger日志模塊詳解

    這篇文章主要介紹了Python中l(wèi)ogger日志模塊詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評論

嘉黎县| 平阳县| 无锡市| 临江市| 错那县| 高清| 上思县| 突泉县| 平度市| 弥渡县| 巨鹿县| 都兰县| 团风县| 洛宁县| 惠东县| 麻城市| 河东区| 蓬安县| 广灵县| 安塞县| 奇台县| 监利县| 平果县| 平阴县| 灵台县| 三亚市| 滦南县| 阆中市| 卢氏县| 琼结县| 衡山县| 宁津县| 柏乡县| 观塘区| 高要市| 宜兴市| 内丘县| 万宁市| 绥阳县| 灯塔市| 祥云县|