【问题标题】:Need to get the first image link with Python需要用Python获取第一个图片链接
【发布时间】:2021-07-10 23:10:45
【问题描述】:

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

我正在使用 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

【问题讨论】:

    标签: python web-scraping beautifulsoup scrapy urllib


    【解决方案1】:

    您看到的数据是通过 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
    

    【讨论】:

    • 工作正常!!但是我怎样才能访问第一个链接first_photo = g["file"][0]?
    • @ÒscarFuentes 要获取第一个网址,您可以first = data["data"]["request"][0]["ships"][0]["data"]["gallery"][0]["file"]
    • 并且可以将照片的链接用作png或jpg链接吗?我正在用 Folium 制作地图,所以我想在弹出窗口中显示照片。谢谢大家!
    • @ÒscarFuentes 图片已经是 JPG,所以 &lt;img src="https://photos.marinetraffic.com/ais/showphoto.aspx?photoid=2948097" /&gt; 应该可以工作。
    • 已解决! try: image = data["data"]["request"][0]["ships"][0]["data"]["gallery"][0]["file"] except IndexError: image= 'null'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2012-05-16
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多