scrapy爬取公交站

scrapy爬取公交站
1、settings

ITEM_PIPELINES = {
   'test2.pipelines.Test2Pipeline': 300,
}

2、items

class Test2Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    bus_name=scrapy.Field()
    bus_type=scrapy.Field()
    bus_time=scrapy.Field()
    bus_cost=scrapy.Field()
    bus_update=scrapy.Field()

3、spiders

from test2.items import Test2Item
import scrapy
import logging


from lxml import etree
import requests
import time

class Test2ItemSpider(scrapy.Spider):
    name="test2Item"
    city_name=input("请输入要爬取公交站地址的城市名称:--")
    allowed_domains=['{}.8684.cn'.format(city_name)]

    start_urls={
        "https://{}.8684.cn/".format(city_name),
    }
    def parse(self,response):
        # filename="car.html"
        # open(filename,"wb").write(response.body)
        items=[]
        test2Item=Test2Item()

        # /list1
        a_list=response.xpath("//div[@class='bus-layer depth w120']/div[@class='pl10'][1]/div[@class='list']/a/@href").extract()

        # https://lanzhou.8684.cn/list1
        all_urls=[response.url+a.strip("/") for a in a_list]#url前缀列表


        print('*' * 10)
        print(response.url)
        print('*' * 10)
        # 获取详情页后缀,和详情信息
        for a in all_urls:
            tree=etree.HTML(requests.get(url=a).text)
            detail_href= tree.xpath("//div[@class='list clearfix']/a/@href")
            for detail in detail_href:
                test2Item = Test2Item()
                all_urls_1=response.url+detail.strip("/")
                tree = etree.HTML(requests.get(url=all_urls_1).text)
                # print('*' * 10)
                time.sleep(2)
                bus_name = tree.xpath("//div[@class='layout-left']/div[@class='bus-lzinfo mb20']//h1/text()")[0]
                test2Item["bus_name"] = bus_name
                #公交类型
                bus_type = tree.xpath("//div[@class='layout-left']/div[@class='bus-lzinfo mb20']//h1/a/text()")[0]
                test2Item["bus_type"]=bus_type
                #公交时间
                bus_time = tree.xpath("//div[@class='layout-left']/div[@class='bus-lzinfo mb20']//ul/li[1]/text()")[0].lstrip("运行时间:")
                test2Item["bus_time"]=bus_time
                #公交花费
                bus_cost = tree.xpath("//div[@class='layout-left']/div[@class='bus-lzinfo mb20']//ul/li[2]/text()")[0].lstrip("票价信息:")
                test2Item["bus_cost"]=bus_cost
                #公交更新时间
                bus_update= tree.xpath("//div[@class='layout-left']/div[@class='bus-lzinfo mb20']//ul/li[4]/text()")[0].lstrip("最后更新:")
                test2Item["bus_update"] = bus_update
                items.append(test2Item)
        return items

4、piplines

import json

class Test2Pipeline(object):
    def open_spider(self, spider):  # 在爬虫开启的时候仅执行一次
        if spider.name == 'test2Item':
            self.f = open('json1.txt', 'a', encoding='utf-8')

    def close_spider(self, spider):  # 在爬虫关闭的时候仅执行一次
        if spider.name == 'test2Item':
            self.f.close()

    def process_item(self, item, spider):
        # 想输出真正的中文 ensure_ascii=False
        if spider.name == 'test2Item':
            self.f.write(json.dumps(dict(item), ensure_ascii=False, indent=2) + ',\n')
        # 不return的情况下,另一个权重较低的pipeline将不会获得item
        return item

5、结果
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值