如何在字符串中更快地获得某些斜线后的数字?

时间:2018-01-31 22:26:44

标签: python python-3.x web-scraping

在此链接中,我想从网址获取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])

1 个答案:

答案 0 :(得分:1)

试一试:

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