scrapy爬取豆瓣電影

修改了豆瓣電影的名字個(gè)數(shù)以及利用正則表達(dá)式只留下評(píng)分人數(shù)中的數(shù)字

# -*- coding: utf-8 -*-
# import scrapy
from scrapy.spiders import CrawlSpider
from scrapy.http import Request
from douban.items import DoubanItem
import urllib
import re


class DoubanMovieTop250Spider(CrawlSpider):
    name = 'douban'  # 此處name必須與項(xiàng)目名字一致
    allowed_domians = ['douban.com']

    def start_requests(self):
        # , headers={'User_Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'})
        headers = {
            # 'USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
            'referer': 'https://www.baidu.com/link?url=7u5fi9zxZ7UhcghOj540fjWpmi_nT2hX2EEpXKItLyv78gYcl3zlgHfVo15ZkzTY&wd=&eqid=d2ac1cbe001b4b8c0000000358fb62bf'
        }
        yield Request("https://movie.douban.com/tag/", headers=headers, callback=self.tag_page)

    def tag_page(self, response):
        alltag_url = response.xpath(
            '//*[contains(@class,"tagCol")]//@href').extract()
        for url in alltag_url:
            yield Request(urllib.parse.urljoin(response.url, url), callback=self.index_page)

    def index_page(self, response):
        selector_nextpage = response.xpath(
            '//*[contains(@class,"next")]/a/@href').extract()
        # //*[contains(@class,"next")]/a/@href與//*[contains(@class,"next")]//@href效果一樣
        for url in selector_nextpage:
            yield Request(urllib.parse.urljoin(response.url, url), callback=self.index_page)

        selector_items = response.xpath(
            '//tr[@class="item"]')
        for item in selector_items:
            yield self.parse_item(item, response)

    def parse_item(self, selector, response):
        movie = DoubanItem()
        # title1 = selector.xpath(
        #     './/*[@class="pl2"]/a/text()').extract()
        # title2 = selector.xpath(
        #     './/*[@class="pl2"]/a/span/text()').extract()
        # if title2 == []:
        #     for i in range(len(title1)):
        #         t1 = title1[i]
        #     t1 = t1.strip()
        #     movie['title'] = [t1]
        # if title2 == []:
        #     t1 = title1[0].strip()
        #     t1 = t1.strip()
        #     movie['title'] = [t1]
        # else:
        #     movie['title'] = title2
        t = selector.xpath(
            './/*[@class="pl2"]/a/text()').extract()[0].replace('/', '').strip()
        # 將xpath獲取的標(biāo)題有tuple轉(zhuǎn)為字符串,再將字符串末尾中的'/'替換為空格,最后使用strip函數(shù)去掉字符串首尾的空格和換行符
        movie['title'] = [t]  # 將處理好的字符轉(zhuǎn)換成tuple賦值給movie['title']
        # movie['title'] = selector.xpath(
        #     './/*[@class="pl2"]/a/text()').extract()[0]  # .replace('/', '').strip()
        movie['star'] = selector.xpath(
            './/*[@class="rating_nums"]/text()').extract()
        num1 = selector.xpath(
            './/span[@class="pl"]/text()').extract()[0]
        num2 = re.findall(r"\d+", num1)  # 正則xpath提取的評(píng)分人數(shù),只獲取數(shù)值
        movie['score']=num2
      #  for _, num in enumerate(num2):
       #   num = num.group()
        #  movie['score'] = [str(num)]
        # movie['score']= selector.xpath(
        #     './/span[@class="pl"]/text()').extract()
        return movie
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這兩天摸索了下scrapy,剛看文檔的時(shí)候覺得有點(diǎn)生無可戀,scrapy框架個(gè)人還是覺得比較難懂的,需要學(xué)習(xí)的地方...
    Treehl閱讀 5,853評(píng)論 7 10
  • 目標(biāo)網(wǎng)站:http://movie.douban.com/top250 目標(biāo)內(nèi)容: 電影名稱 電影信息 電影評(píng)分 ...
    兔頭咖啡閱讀 359評(píng)論 0 0
  • 前言 需要進(jìn)行表單數(shù)據(jù)驗(yàn)證,原先才用html5來完成驗(yàn)證,但是效果很差,也不夠靈活,所以需要進(jìn)行自定義的表單驗(yàn)證,...
    _塵_閱讀 8,104評(píng)論 0 1
  • 經(jīng)常會(huì)有人發(fā)私信問我,阿紫,你能給當(dāng)代的大學(xué)生提些建議么?在復(fù)雜的人際關(guān)系中,如何少走彎路,少吃虧? 我告訴你,請(qǐng)...
    文藝女青年專治各種不服閱讀 2,929評(píng)論 5 17
  • 唐朝中后期開始,皇室的權(quán)利逐漸不足以控制藩鎮(zhèn)勢力,于是中國進(jìn)入了長達(dá)百年的分裂時(shí)期,此時(shí)的中原地區(qū)先后出現(xiàn)了由“朱...
    碩鼠無止閱讀 619評(píng)論 0 1

友情鏈接更多精彩內(nèi)容