项目简介:
爬取网页:全国火车列车时刻表在线查询 最新火车时刻表 动车高铁时刻表查询 查列车
使用Scrapy框架爬取该网页两地之间当天的火车信息,并将爬取到的信息保存在MySQL数据库中。
项目流程:
爬取数据:
1.创建Scrapy框架:
2.编写爬虫项目:
首先利用字符串拼接的方式传递URL,实现简单的动态查询,做到可以查寻任意两地的信息。
import scrapy
class ChaliecheSpider(scrapy.Spider):
name = "chalieche"
allowed_domains = ["www.chalieche.com"]
a = input('出发地:')
b = input('目的地:')
c="https://www.chalieche.com/search/range/?from="
d="&to="
start_urls = [
c+a+d+b
]
3.通过Xpath路径的方式获取页面中的文字信息:
def parse(self, response):
# 车次
TrainNumber = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[1]/a/b/text()"
).getall()
# 列车类型
TrainType = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[2]/text()"
).getall()
# 始发站
DepartureStation = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[3]/a/text()"
).getall()
# 终点站
TerminalStation = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[4]/a/text()"
).getall()
# 乘车站
TrainToStation = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[5]/a/text()"
).getall()
# 发车时间
DepartureTime1 = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[6]/span[1]/text()"
).getall()
DepartureTime2 = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[6]/span[2]/text()"
).getall()
# 到达站
ArrivalStation = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[7]/a/text()"
).getall()
# 到达时间
ArrivalTime1 = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[8]/span[1]/text()"
).getall()
ArrivalTime2 = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[8]/span[2]/text()"
).getall()
# 耗时
TimeConsuming = response.xpath(
"/html/body/div[1]/div[2]/div/div[3]/div/div/table/tbody/tr/td[9]/span/text()"
).getall()
3.将获取到的数据通过yield方法返回出去:
for t in range(len(TrainNumber)):
yield{
'车次':TrainNumber[t],
'列车类型':TrainType[t],
'始发站':DepartureStation[t],
'终点站':TerminalStation[t],
'出发站':TrainToStation[t],
'发车时间':DepartureTime1[t]+DepartureTime2[t],
'到达站':ArrivalStation[t],
'到达时间':ArrivalTime1[t]+ArrivalTime2[t],
'耗时':TimeConsuming[t]
}
将数据保存至MySQL数据库:
通过爬取我们已经获取到了数据接下来我们需要将数据保存到数据库中:
首先我们需要开启一个管道:
修改settings.py文件:
随后我们就可以连接数据库并通过sql语句将数据保存至MySql数据库中:
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
class MysqlPipeline:
def open_spider(self,spider):
//自行修改自己数据库信息:
self.client=pymysql.connect(host='*********',port=****,user='******',passwd='********',db='****',charset='utf8')
self.cousor=self.client.cursor()
def process_item(self,item,spider):
args=[
item.get('车次'),
item.get('列车类型'),
item.get('始发站'),
item.get('终点站'),
item.get('出发站'),
item.get('发车时间'),
item.get('到达站'),
item.get('到达时间'),
item.get('耗时'),
]
//自行修改sql语句
sql='insert into ****** values (0,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
self.cousor.execute(sql,args)
self.client.commit()
return item
def close_spider(self,spider):
self.cousor.close()
self.client.close()
运行项目:
进入项目所在文件夹后输入scrapy crawl chalieche(根据自己文件名修改命令)命令运行程序
运行成功后刷新数据库即可获取数据: