简体   繁体   English

在python中动态生成URL

[英]Generate dynamically URL in python

I would like to programatically generate a URL made by these parts 我想以编程方式生成由这些部分组成的URL

Fixed part 固定部分

https://booking.snav.it/#/booking/rates/

Outbound route number - changes 出站路由号-更改

1040

Outbound date - changes 出站日期-更改

19-02-2019

Inbound route number - changes 入站路由号-更改

1042

Inbound date - changes 入站日期-更改

20-02-2019

Parameters: 参数:

"adults": "1"
"childs":"0"
"infants":"0"
"res": "0"
"vehicle":"0"

Output 产量

https://booking.snav.it/#/booking/rates/1040/19-02-2019/1042/19-02-2019?adults=1&childs=0&infants=0&res=0&vehicle=0

I know how to pass parameters with urllib.parse.urlencode 我知道如何使用urllib.parse.urlencode传递参数

params = urllib.parse.urlencode({
   "adults": "1"
    "childs":"0"
    "infants":"0"
    "res": "0"
    "vehicle":"0"
})

url = "https://booking.snav.it/#/booking/rates/"
res = requests.get(url, params=params)

but don't know how to build dynamically the first part after the fixed URL 1040/19-02-2019/1042/19-02-2019 但不知道如何在固定网址1040/19-02-2019/1042/19-02-2019之后动态构建第一部分

A URL is really just a string, any of the usual string manipulation techniques would do here. URL实际上只是一个字符串,任何常用的字符串操作技术都可以在这里完成。 Your component parts don't have any characters in them that would require URL-encoding here either, making the whole process simpler. 您的组成部分中也没有任何字符,也不需要在此处进行URL编码,从而使整个过程更加简单。

If you do have component parts that use characters that are not in the list of unreserved characters , then use the urllib.parse.quote() function to convert those characters to URL-safe components. 如果确实有使用不属于保留字符列表中的字符的组成部分,请使用urllib.parse.quote()函数将那些字符转换为URL安全组件。

You could use str.join() with / to join string parts: 您可以将str.join()/结合使用,以连接字符串部分:

outbound_route = '1040'
outbound_date = '19-02-2019'
inbound_route = '1042'
inbound_date = '20-02-2019'

url = "https://booking.snav.it/#/booking/rates"  # no trailing /
final_url = '/'.join([url, outbound_route, outbound_date, inbound_route, inbound_date])

or you could use a formatted string literal : 或者您可以使用格式化的字符串文字

url = "https://booking.snav.it/#/booking/rates/"
final_url = f'{url}{outbound_route}/{outbound_date}/{inbound_route}/{inbound_date}'

This approach has the advantage that the components don't have to be strings; 这种方法的优点是组件不必是字符串。 if outbound_route and inbound_route are integers, you don't have to explicitly convert them to strings first. 如果outbound_routeinbound_route是整数,则不必先将它们显式转换为字符串。

Or, since URL paths work a lot like POSIX filesystem paths, you could use the pathlib.PosixPurePath() class to contruct the path: 或者,由于URL路径的工作方式与POSIX文件系统路径非常相似,因此可以使用pathlib.PosixPurePath()构造路径:

from pathlib import PosixPurePath

path = PosixPurePath('/booking/rates') / outbound_route / outbound_date / inbound_route / inbound_date
final_url = f"https://booking.snav.it/#{path}"

In all cases, you end up with a final URL to use in requests : 在所有情况下,最终都有一个最终URL用于requests

res = requests.get(final_url, params=params)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM