简体   繁体   English

Python 从 n 元素中获取字符到最后包含的最后一个

[英]Python get character from n element to last included last

link_element = """a target="_top" aria-current="false" class="Anchor NavList__Anchor" href="/tr/turnuva-konumu/y%C3%BCksek-oran/101/d%C3%BCnya/240/sk-slavia-prague-rak%C3%B3w-cz%C4%99stochowa-hnk-hajduk-split-villarreal-cf-cfr-cluj-nk-maribor-%C5%A1k-slovan-bratislava-h%C5%A1k-zrinjski-mostar/***180254557458746932***">xxx</a"""

uniqueid = link_element['href'][-18:-1]

When i try to get the integer value of the link it returns with bs4 and requests libs 18025455745874693.It does not include the last char.当我尝试获取链接的 integer 值时,它使用 bs4 返回并请求库 18025455745874693。它不包括最后一个字符。 How can i get the full integer value of the href?如何获得完整的 integer 值?

In this case you can just replace [-18:-1] with [-18:] .在这种情况下,您只需将[-18:-1]替换为[-18:]即可。 Using -1 as your ending index excludes the last character.使用-1作为结束索引会排除最后一个字符。

However, in the case where this ID's length is variable (ie. may not always be 18 characters), the better option would be to split your string by / and grab the last element.但是,如果此 ID 的长度是可变的(即,可能并不总是 18 个字符),更好的选择是用/分割字符串并获取最后一个元素。

unique_id = link_element['href'].split("/")[-1]

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

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