生成一个抓取船讯网的数据的代码\
时间: 2024-03-29 10:38:40 浏览: 21
以下是一个基于Python的示例代码,用于从船讯网(http://www.shipxy.com/)抓取数据:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.shipxy.com/ship/getShipData.do'
params = {
'mmsi': 'xxxx', # 替换成您要查询的船舶的MMSI号码
'type': 'ship',
'dataType': 'jsonp',
'_': '1234567890' # 替换成当前的时间戳
}
response = requests.get(url, params=params)
data = response.content.decode('utf-8')
data = data[data.index('{'):data.rindex('}')+1] # 从响应数据中提取JSON数据部分
data = data.replace('\\', '') # 去除转义字符
soup = BeautifulSoup(data, 'html.parser')
ship_info = soup.find('div', {'class': 'ship_info'})
print(ship_info.text.strip()) # 输出船舶信息
```
请注意,该代码仅供参考,具体实现可能因网站数据结构和页面变化而略有不同。此外,网站的数据抓取可能会违反其使用协议,请在使用前务必仔细阅读网站的相关条款。