需要使用Python获得第一个图像链接

2024-10-01 13:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从链接中获取第一张照片的链接“https://www.balticshipping.com/vessel/imo/9127382“使用Python

我正在用BeautifullSoup库进行测试,但是没有办法得到它。从我所看到的,图像不是JPG或PNG格式,因此,它没有检测到它

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen('https://www.balticshipping.com/vessel/imo/9127382')
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src':re.compile('.png')})
for image in images: 
    print(image['src']+'\n')

有人知道怎么做吗

完整循环代码:(“s”包含许多船舶数据(IMO、日期、船名…)

def create_geojson_features(s):

features = []

for _, row in s.iterrows():
    vessel_id = row['IMO']

    data = {
        "templates[]": [
            "modal_validation_errors:0",
            "modal_email_verificate:0",
            "r_vessel_types_multi:0",
            "r_positions_single:0",
            "vessel_profile:0",
        ],
        "request[0][module]": "ships",
        "request[0][action]": "list",
        "request[0][id]": "0",
        "request[0][data][0][name]": "imo",
        "request[0][data][0][value]": vessel_id,
        "request[0][sort]": "",
        "request[0][limit]": "1",
        "request[0][stamp]": "0",
        "request[1][module]": "top_stat",
        "request[1][action]": "list",
        "request[1][id]": "0",
        "request[1][data]": "",
        "request[1][sort]": "",
        "request[1][limit]": "",
        "request[1][stamp]": "0",
        "dictionary[]": ["countrys:0", "vessel_types:0", "positions:0"],
    }
    
    data = requests.post("https://www.balticshipping.com/", data=data).json()
    image = data["data"]["request"][0]["ships"][0]["data"]["gallery"][0]["file"]
    print(image)
    feature = {
        'type': 'Feature',
        'geometry': {
            'type':'Point',
            'coordinates':[row['lon'],row['lat']]
        },
        'properties': {
            'time': pd.to_datetime(row['date']).__str__(),
            'popup': "<img src=" + image.__str__() + " width = '250' height='200'/>"+'<br>'+'<br>'+'Shipname: '+row['shipname'].__str__() +'<br>'+ 'MMSI: '+row['mmsi'].__str__() +'<br>' + 'Group: '+row['group'].__str__() +'<br>''Speed: '+row['speed'].__str__()+' knots',
            'style': {'color' : ''},
            'icon': 'circle',
            'iconstyle':{
                'fillColor': row['fillColor'],
                'fillOpacity': 0.8,
                'radius': 5
            }
        }
    }
    features.append(feature)
return features

Tags: httpsimagebrimportcomiddatarequest
1条回答
网友
1楼 · 发布于 2024-10-01 13:27:20

您看到的数据是通过Ajax从外部源加载的。您可以使用此示例了解如何获取图片URL:

import json
import requests


url = "https://www.balticshipping.com/vessel/imo/9127382"
vessel_id = url.split("/")[-1]

data = {
    "templates[]": [
        "modal_validation_errors:0",
        "modal_email_verificate:0",
        "r_vessel_types_multi:0",
        "r_positions_single:0",
        "vessel_profile:0",
    ],
    "request[0][module]": "ships",
    "request[0][action]": "list",
    "request[0][id]": "0",
    "request[0][data][0][name]": "imo",
    "request[0][data][0][value]": vessel_id,
    "request[0][sort]": "",
    "request[0][limit]": "1",
    "request[0][stamp]": "0",
    "request[1][module]": "top_stat",
    "request[1][action]": "list",
    "request[1][id]": "0",
    "request[1][data]": "",
    "request[1][sort]": "",
    "request[1][limit]": "",
    "request[1][stamp]": "0",
    "dictionary[]": ["countrys:0", "vessel_types:0", "positions:0"],
}

data = requests.post("https://www.balticshipping.com/", data=data).json()

# uncomment to print all data:
# print(json.dumps(data, indent=4))

for g in data["data"]["request"][0]["ships"][0]["data"]["gallery"]:
    print(g["file"])

印刷品:

https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2948097
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2864147
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2830344
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2674783
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2521379
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2083722
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2083721
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1599301
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1464102
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1464099
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1464093
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1464089
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=1110349
https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=433106

相关问题 更多 >