python获取船讯网船讯信息
时间: 2023-08-12 14:07:35 浏览: 67
要获取船讯网的船讯信息,你可以使用Python和网络爬虫技术。下面是一个简单的示例代码,使用第三方库BeautifulSoup和requests来实现:
```python
import requests
from bs4 import BeautifulSoup
def get_ship_info():
# 构建请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
}
# 发送请求并获取网页内容
url = 'http://www.shipxy.com/Ship/List?flag=1&keyWord='
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页内容,获取船讯信息
ship_info = []
ship_list = soup.find_all('div', class_='ship-item')
for ship in ship_list:
ship_name = ship.find('div', class_='ship-name').text.strip()
ship_type = ship.find('div', class_='ship-type').text.strip()
ship_info.append({'船名': ship_name, '类型': ship_type})
return ship_info
# 调用函数获取船讯信息
result = get_ship_info()
for ship in result:
print(ship)
```
这段代码会从船讯网上获取船名和类型信息,并将其以字典形式存储在列表中,最后打印出来。你可以根据自己的需求进行进一步的处理和解析。注意,爬取网页信息时要遵守网站的使用规则和法律法规。