如何在字符串中以更快的速度获取特定斜杠后的数字?

2024-05-29 10:10:46 发布

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

在这个链接中,我想从url获取302621221479,我能更快地完成吗

我要求的是学习如何改进我的代码,而不是如何去做

url = 'https://www.ebay.com/itm/Deep-V-Neck-Sequin-Bodycon-Cocktail-Dress-Women-Clubwear-Sexy-Party-Dress-/302621221479?var=&hash=item8c09eb2518'

u = url.split('/')

id_product = u[-1].split('?')

print(id_product[0])

Tags: 代码httpscomidurl链接wwwproduct
1条回答
网友
1楼 · 发布于 2024-05-29 10:10:46

试一试:

URL = 'https://www.ebay.com/itm/Deep-V-Neck-Sequin-Bodycon-Cocktail-Dress-Women-Clubwear-Sexy-Party-Dress-/302621221479?var=&hash=item8c09eb2518'

product_id = URL.split("/")[-1].split('?')[0] ##you can try like this
product_id = URL.split('?')[0].split("/")[-1]  ##this one is safer
print(product_id)

输出:

302621221479

相关问题 更多 >

    热门问题