在船讯网通过船舶的MMSI获取船舶的信息,话不多说,直接上代码:
import random
import time
import csv
import requests
from fake_useragent import UserAgent
class ShipxySpider(object):
def __init__(self):
self.main_url = 'http://www.shipxy.com/'
self.mmsi_url = 'http://www.shipxy.com/Advert/JinGangJingShips'
self.data_url = 'http://www.shipxy.com/ship/GetShip'
self.ua = UserAgent()
self.header = {'User-Agent': self.ua.chrome}
def getData(self):
session = requests.Session()
session.get(self.main_url, headers=self.header)
mmsi_res = session.post(self.mmsi_url, headers=self.header)
MMSI = mmsi_res.json()['data']
alldata = []
for mmsi in MMSI:
data = {"mmsi": mmsi}
data_res = session.post(self.data_url, headers=self.header, data=data)
print(data_res.json()['data'])
alldata.append(data_res.json()['data'])
session.close()
return alldata
def saveData(self, filename):
with open(filename, mode='w', newline='') as csv_file:
fieldnames = ['mmsi', 'lat', 'lon', 'tradetype', 'callsign', 'hdg', 'trail', 'laststa', 'lastdyn', 'imo',
'satelliteutc', 'type', 'left', 'length', 'matchtype', 'draught', 'dest', 'width', 'name',
'cog', 'rot', 'navistatus', 'cnname', 'source', 'sog', 'eta', 'shipid']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for item in self.getData():
writer.writerows(item)
def run(self):
self.saveData('data.csv')
time.sleep(random.randint(1, 2))
if __name__ == '__main__':
start = time.time()
spider = ShipxySpider()
spider.run()
end = time.time()
print('执行时间:%.2f' % (end - start))
结果:
|