首页 > 其他分享 >数据采集与融合技术实践第三次实验

数据采集与融合技术实践第三次实验

时间:2023-10-20 17:36:05浏览次数:45  
标签:xpath Field 第三次 item self get 融合 采集 scrapy

数据采集与融合技术实践第三次实验

Gitee:https://gitee.com/lululusc/crawl_project/tree/master/作业3

作业1

要求

指定一个网站,爬取这个网站中的所有的所有图片,例如中国气象网(http://www.weather.com.cn/(要求:指定--个网站,爬取这个网站中的所有的所有图片,例如中国气象网)

结果

url信息

图片信息

代码

单线程爬取

这个代码主要是异步执行提高速度的,在pipeline中,使用yield来提交Request请求。

weather.py

class WeatherSpider(scrapy.Spider):
    name = "weather"
    allowed_domains = ["www.weather.com.cn"]
    start_urls = ["http://www.weather.com.cn/"]

    def parse(self, response):
        try:
            imags=response.xpath("//img/@src").extract()
            for img_url in imags:
                weather_img = Lab3Item()
                weather_img['Img_url']=img_url
                yield weather_img
        except Exception as err:
            print(err)

cmdline.execute("scrapy crawl weather -s LOG_ENABLED=False".split())

items

class Lab3Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    Img_url = scrapy.Field()

pipelines

class Lab3Pipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        print(item.get('Img_url'))
        req_url= item.get('Img_url')
        yield Request(req_url)#异步执行

    def file_path(self, request, response=None, info=None,item=None):
        image_guid = hashlib.sha1(to_bytes(request.url)).hexdigest()
        return f'full/{image_guid}.jpg'


    def item_completed(self, results, item, info):
        return item

多线程爬取

在settings中设置

CONCURRENT_REQUESTS = 32

心得

熟悉掌握了多线程与单线程爬取数据

作业2

要求

熟练掌握 scrapy 中 Item、Pipeline 数据的序列化输出方法;使用scrapy框架+Xpath+MySQL数据库存储技术路线爬取股票相关信息。 候选网站:

东方财富网: https://www.eastmoney.com/

新浪股票:http://finance.sina.com.cn/stock/

结果

代码

这次代码使用了selenium+scrapy+xpath抓取,因为页面是动态数据,只用scrapy无法获取。

使用selenium主要是修改了middlewares

1.重写了_init__

def __init__(self):
    service = ChromeService(executable_path=r'C:\Users\洛上尘\Desktop\数据采集\example\lab3_2\lab3_2\spiders\chromedriver.exe')
    self.driver = webdriver.Chrome(service=service)

2.重写process_request

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.
        self.driver.get(request.url)
        body=self.driver.page_source
        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return HtmlResponse(url=self.driver.current_url, body=body, encoding='utf-8', request=request)

在stock.py

class StockSpider(scrapy.Spider):
    name = "stock"
    allowed_domains = ["quote.eastmoney.com"]
    start_urls = ["https://quote.eastmoney.com/center/hszs.html"]

    def parse(self, response,**kwargs):
        lines=response.xpath("//table[@id=\"hszs_hszyzs_simple-table\"]/tbody/tr")
        item=Lab32Item()
        for line in lines:
            item['a1']=line.xpath("./td[1]/text()").extract_first()
            item['a2']= line.xpath("./td[2]/a/text()").extract_first()
            item['a3']= line.xpath("./td[3]/a/text()").extract_first()
            item['a4']= line.xpath("./td[4]/span/text()").extract_first()
            item['a5']= line.xpath("./td[5]/span/text()").extract_first()
            item['a6']= line.xpath("./td[6]/span/text()").extract_first()
            item['a7']= line.xpath("./td[7]/text()").extract_first()
            item['a8']= line.xpath("./td[8]/text()").extract_first()
            item['a9']= line.xpath("./td[9]/text()").extract_first()
            item['a10']= line.xpath("./td[10]/span/text()").extract_first()
            item['a11']= line.xpath("./td[11]/span/text()").extract_first()
            item['a12']= line.xpath("./td[12]/span/text()").extract_first()
            yield item
cmdline.execute("scrapy crawl stock -s LOG_ENABLED=True".split())

items

class Lab32Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    a1 =scrapy.Field()
    a2 =scrapy.Field()
    a3 =scrapy.Field()
    a4 =scrapy.Field()
    a5 =scrapy.Field()
    a6 =scrapy.Field()
    a7=scrapy.Field()
    a8=scrapy.Field()
    a9 =scrapy.Field()
    a10 =scrapy.Field()
    a11=scrapy.Field()
    a12=scrapy.Field()

pipelines

class Lab32Pipeline:
    def open_spider(self,spider):
        print("*******************************************")
        print("opened_爬取1")

        self.mydb = pymysql.connect(
            host="127.0.0.1",
            port=3306,
            user="root",
            password="123456",
            database="crawl",
            charset="utf8"
        )
        self.cursor = self.mydb.cursor()


    def process_item(self, item, spider):
        sql="insert into stock values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        self.cursor.execute(sql,(item.get("a1"),item.get("a2"),item.get("a3"),item.get("a4"),item.get("a6"),item.get("a5"),item.get("a7"),item.get("a8"),item.get("a11"),item.get("a12"),item.get("a10"),item.get("a9")))
        self.mydb.commit()
        return item
    def close_spider(self,spider):
        self.mydb.close()

心得

学会了如何将selenium与scrapy结合起来爬取数据,收获颇多

作业3

要求

熟练掌握 scrapy 中 Item、Pipeline 数据的序列化输出方法;

使用scrapy框架+Xpath+MySQL数据库存储技术路线爬取外汇网站数据。

候选网站:招商银行网:https://www.boc.cn/sourcedb/whpj/

结果

代码

在zhaoshangbank(myspider)里

class ZhaoshangbankSpider(scrapy.Spider):
    name = "zhaoshangbank"
    allowed_domains = ["www.boc.cn"]
    start_urls = ["https://www.boc.cn/sourcedb/whpj/"]

    def parse(self, response):
        trs=response.xpath("//tr")
        trs=trs[2:]
        item=Lab33Item()
        for tr in trs:
            item['name']=tr.xpath("./td[1]/text()").extract_first()
            item['tsp']= tr.xpath("./td[2]/text()").extract_first()
            item['csp']= tr.xpath("./td[3]/text()").extract_first()
            item['tbp']= tr.xpath("./td[4]/text() ").extract_first()
            item['cbp']= tr.xpath("./td[5]/text()").extract_first()
            item['time']= tr.xpath("./td[8]/text()").extract_first()
            print(item.get("name"))
            #yield item
cmdline.execute("scrapy crawl zhaoshangbank -s LOG_ENABLED=False".split())

items

class Lab33Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    name=scrapy.Field() 
    tsp=scrapy.Field() 
    csp=scrapy.Field() 
    tbp=scrapy.Field() 
    cbp=scrapy.Field()  
    time=scrapy.Field()

pipelines

class Lab33Pipeline:
    def open_spider(self,spider):
        self.mydb = pymysql.connect(
        host="127.0.0.1",
        port=3306,
        user="root",
        password="123456",
        database="crawl",
        charset='utf8'
    )
        self.cursor = self.mydb.cursor()

        self.cursor.execute('''CREATE TABLE IF NOT EXISTS zhaoshangbank
                          (Currency VARCHAR(256),
                          TBP VARCHAR(256),
                          CBP VARCHAR(256),
                          TSP VARCHAR(256),
                          CSP VARCHAR(256),
                          times VARCHAR(256)
                           )''')
        self.mydb.commit()

    def process_item(self, item, spider):
        print(item.get("name"))
        sql="INSERT INTO zhaoshangbank (Currency,TBP,CBP,TSP,CSP,times) VALUES (%s,%s,%s,%s,%s,%s)"
        self.cursor.execute(sql,(item.get("name"),item.get("tsp"),item.get("csp"),item.get("tbp"),item.get("cbp"),item.get("time")))
        self.mydb.commit()
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.mydb.close()

心得

这次实验让我懂得了如何利用scrapy+mysql存储数据,更加熟练得操作了mysql,也知道了xpath绝对定位有时候会出错,最好使用迷糊匹配。

标签:xpath,Field,第三次,item,self,get,融合,采集,scrapy
From: https://www.cnblogs.com/lsc-blogs/p/17777589.html

相关文章

  • 如何使用GraalVM和Java采集天涯图片
    今天我要给大家分享的是如何使用GraalVM和Java编写一个采集天涯论坛图片的程序,内容通俗易懂,非常适合新手学习,大神勿喷。```java//导入必要的库importjava.io.*;importjava.net.*;importjava.util.*;//创建一个名为Downloader的类publicclassDownloader{//定义......
  • 工程监测无线振弦采集仪高低温试验箱测试原理
    工程监测无线振弦采集仪高低温试验箱测试原理无线振弦采集仪是一种用来测量结构物动力学特性的仪器,它可以通过振弦传感器采集到结构物的振动信号,并通过数据分析,得到结构物的自然频率、阻尼比、振型等信息。为了确保无线振弦采集仪的准确性和可靠性,需要进行高低温试验,以验证它在各......
  • 工程监测无线振弦采集仪高低温试验箱测试原理
    工程监测无线振弦采集仪高低温试验箱测试原理无线振弦采集仪是一种用来测量结构物动力学特性的仪器,它可以通过振弦传感器采集到结构物的振动信号,并通过数据分析,得到结构物的自然频率、阻尼比、振型等信息。为了确保无线振弦采集仪的准确性和可靠性,需要进行高低温试验,以验证它在各种......
  • 大连理工大学——延期博士、结业博士——毕业生图像采集——拍摄毕业生图像总结
    由于种种原因,导致在校期间一直没有参加拍摄毕业生图像,离校后想着总是要弄个结业证回来的,于是就研究起来这个“毕业生图像采集”的事情来。 由于是离校生,所以没法参加学校组织的统一拍照,不过也给出了一条线上的个人拍摄的途径,下面给出操作步骤。  总的来说,分为两步,第一步是......
  • R语言rcurl爬虫采集抓取问财财经搜索网页股票数据|附代码数据
    原文参考:http://tecdat.cn/?p=4560 最近我们被客户要求抓取问财财经搜索网页股票数据,包括一些图形和统计输出。问财财经搜索是同花顺旗下的服务之一,主要针对上市公司的公告、研报、即时新闻等提供搜索及参考资料。相对于其他股票软件来说,一个强大之处在于用自然语言就可以按你......
  • 大规模语言LLaVA:多模态GPT-4智能助手,融合语言与视觉,满足用户复杂需求
    大规模语言LLaVA:多模态GPT-4智能助手,融合语言与视觉,满足用户复杂需求一个面向多模式GPT-4级别能力构建的助手。它结合了自然语言处理和计算机视觉,为用户提供了强大的多模式交互和理解。LLaVA旨在更深入地理解和处理语言和视觉信息,从而实现更复杂的任务和对话。这个项目代表了下一......
  • 低代码如何赋能实体经济走向数实融合
    数字工厂、智慧园区、智慧社区、数字乡村、智慧校园、数字化管理平台……当前,各行各业正在全面拥抱数字化。“数实融合”的浪潮持续高涨,企业数字化转型的呼声也不断走高。然而,当前实体经济和数字经济融合不全,数字技术未能深度渗透到产品制造各主要环节,严重制约了企业数字转型的效......
  • Prometheus 都可以采集那些指标?-- 常用 Exporter 合集
    Prometheus可以通过各种Exporter来获取很多指标,并且只要符合Prometheus规范的都可以获取到,本文汇总一些常用的采集器到这里。  PrometheusExporter(一)NodeExporter  PrometheusExporter(二)WindowsExporter  PrometheusExporter(三)cAdvisor  Prometheus......
  • 数据采集IO卡 软件无线电处理平台:136-KC705E增强版基于FMC接口的 Kintex-7 XC7K325T P
    一、板卡概述   本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_to_pin兼容FPGAXC7K410T-2FFG900 ,支持PCIeX8、64bit DDR3容量2GByte,HPC的FMC连接器,板卡支持各种接口输入,软件支持windows,Linux驱动。   二、功能和技术指标: 板卡功能参数内容主处理器Xi......
  • 数据采集与融合实践二
    作业一.天气预报1.内容代码如下:importrequestsfrombs4importBeautifulSoupimportsqlite3importlogginglogging.basicConfig(level=logging.INFO)classWeatherDB:def__init__(self,db_name):self.db_name=db_namedef__enter__(self):......