python3+scrapy+selenium爬取英雄联盟英雄资料

继前一篇文章用nodejs+puppeteer+chromium爬取了这个英雄资料后,在本篇同样爬这个页面,思路都差不多,只是用不同语言来实现,可作为参考,个人觉得爬虫还是nodejs比较好用,可能是我python太菜吧

本例环境和所需第三方包:python3、pycharm、selenium2.48.0(用3.0+版本会报错,因为新版本放弃phantomjs了,当然也可以用chrome和firefox,不过可能需要另外装驱动)、scrapy1.6.0

对于还没了解过scrapy和phantomjs的可以先看下这两篇博客,写的很详细:

https://www.cnblogs.com/kongzhagen/p/6549053.html

https://jiayi.space/post/scrapy-phantomjs-seleniumdong-tai-pa-chong

 

安装就不多说了,可以先装scrapy,如果有报错需要什么再装什么,一般windos还需要安装一个pywin32

创建项目:

scrapy startproject loldocument
cd loldocument
scrapy genspider hero lol.qq.com/data/info-heros.shtml

 

然后生成目录:

 

spiders/hero.py发起列英雄表页的请求获取每个英雄所对应的详情页url,循环访问每个url,然后得到想要的数据:

# -*- coding: utf-8 -*-
import scrapy
from loldocument.items import LoldocumentItem
import os
import urllib.request
import re
class HeroSpider(scrapy.Spider):
    name = 'hero'
    allowed_domains = ['lol.qq.com']
    start_urls = ['https://lol.qq.com/data/info-heros.shtml']

    def parse(self, response):
        heros = response.xpath('//ul[@id="jSearchHeroDiv"]/li')
        # heros = [heros[0], heros[1]]
        for hero in heros:  # 遍历每个li
            imgu = 'http:' + hero.xpath("./a/img/@src").extract_first()
            title = hero.xpath("./a/@title").extract_first()
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
            req = urllib.request.Request(url=imgu, headers=headers)
            res = urllib.request.urlopen(req)
            path = r'F:\loldocument\hero_logo' # 保存英雄头像的路径
            if not os.path.exists(path):
                os.makedirs(path)
            file_name = os.path.join(r'F:\loldocument\hero_logo', title + '.jpg')
            with open(file_name, 'wb') as fp:
                fp.write(res.read())
            url = 'https://lol.qq.com/data/' + hero.xpath("./a/@href").extract_first()
            request = scrapy.Request(url=url, callback=self.parse_detail)
            request.meta['PhantomJS'] = True
            request.meta['title'] = title
            yield request

    def parse_detail(self, response):
        # 英雄详情
        item = LoldocumentItem()
        item['title'] = response.meta['title']
        item['DATAname'] = response.xpath('//h1[@id="DATAname"]/text()').extract_first()
        item['DATAtitle'] = response.xpath('//h2[@id="DATAtitle"]/text()').extract_first()
        item['DATAtags'] = response.xpath('//div[@id="DATAtags"]/span/text()').extract()
        infokeys = response.xpath('//dl[@id="DATAinfo"]/dt/text()').extract()
        infovalues = response.xpath('//dl[@id="DATAinfo"]/dd/i/@style').extract()
        item['DATAinfo'] = {} # 英雄属性
        for i,v in enumerate(infokeys):
            item['DATAinfo'][v] = re.sub(r'width:', "", infovalues[i])
        yield item

 

items.py定义要存入item的字段:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy

class LoldocumentItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()
    DATAname = scrapy.Field()
    DATAtitle = scrapy.Field()
    DATAtags = scrapy.Field()
    DATAinfo = scrapy.Field()

 

因为用selenium爬取异步数据所以需要另外单独定义一个下载器中间件,在新建目录和文件,在/loldocument新建python package middlware.py:

from selenium import webdriver
from scrapy.http import HtmlResponse
import time

class JavaScriptMiddleware(object):
    def process_request(self, request, spider):
        print("PhantomJS is starting...")
        driver = webdriver.PhantomJS()  # 指定使用的浏览器
        # driver = webdriver.Firefox()
        driver.get(request.url)
        time.sleep(1)
        if 'PhantomJS' in request.meta :
            js = "var q=document.documentElement.scrollTop=1000"
            driver.execute_script(js)  # 可执行js,模仿用户操作。此处为将页面拉至最底端。
            time.sleep(1)
            body = driver.page_source
            print("访问详情页" + request.url)
            return HtmlResponse(driver.current_url, body=body, encoding='utf-8', request=request)
        else:
            js = "var q=document.documentElement.scrollTop=1000"
            driver.execute_script(js)  # 可执行js,模仿用户操作。此处为将页面拉至最底端。
            time.sleep(1)
            body = driver.page_source
            print("访问:" + request.url)
            return HtmlResponse(driver.current_url, body=body, encoding='utf-8', request=request)

 

settings.py修改三个配置:

ROBOTSTXT_OBEY = False

DOWNLOADER_MIDDLEWARES = {
    'loldocument.middlewares.middleware.JavaScriptMiddleware': 543, #键为中间件类的路径,值为中间件的顺序
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware':None, #禁止内置的中间件
}

ITEM_PIPELINES = {
   'loldocument.pipelines.LoldocumentPipeline': 100,
}

 

数据处理pipelines.py:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json

class LoldocumentPipeline(object):
    def process_item(self, item, spider):
        with open('hero_detail.txt', 'a') as txt:
            str = json.dumps(dict(item), ensure_ascii=False)
            txt.write(str)

执行 scrapy crawl hero 执行程序,同时命令窗会输出详情及异常信息,加--nolog可以不输出详情

得到的数据hero_detail.txt和英雄头像hero_logo目录:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Scrapy框架结合Selenium进行新闻爬取的分析过程如下: 1. 确定目标:确定要爬取的新闻网站,并了解其页面结构、数据位置以及需要的数据类型。 2. 配置Scrapy项目:创建一个Scrapy项目,包括建立项目目录结构、配置爬虫规则、编写爬虫代码等。 3. 配置Selenium:将Selenium集成到Scrapy项目中,通过pip安装Selenium库,并下载相应的浏览器驱动程序(如ChromeDriver)。 4. 编写爬虫代码:在Scrapy项目中的爬虫代码中,创建一个爬虫类,并在该类中定义网站链接的解析方法和数据提取规则。 5. 使用Selenium进行网页交互:在网页解析方法中,使用Selenium启动浏览器,并访问目标网址,以便进行网页的渲染和加载,确保获取到动态生成的数据。 6. 分析网页结构和数据位置:使用Selenium的相关方法定位并提取所需数据的位置,并将其保存到Scrapy的Item对象中。 7. 数据持久化:将提取到的数据保存到数据库或者以其他方式进行持久化,可以使用Scrapy提供的Pipeline功能。 8. 运行爬虫:运行Scrapy项目,爬虫会按照预定的规则进行网页解析,并将数据提取、保存、持久化。 9. 数据分析:根据需求进行数据分析,可以使用Python的数据分析工具(如Pandas、Numpy等)对爬取到的新闻数据进行统计、处理、可视化等操作。 10. 定期维护:定期监控目标网站的变化,更新爬虫代码,以确保爬虫的正常运行和数据的准确性。 通过使用Scrapy框架结合Selenium进行新闻爬取,我们可以实现对需要JavaScript渲染的网站的爬取,提取所需数据并进行后续的数据分析。同时,Scrapy提供了强大的爬虫功能,包括自动处理请求、解析网页、处理数据等,能够提高爬虫的效率和稳定性。而Selenium的集成则可以保证获取到完整的动态生成的数据,使爬虫更具灵活性和适应性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值