简体   繁体   中英

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. How can i get the full integer value of the href?

In this case you can just replace [-18:-1] with [-18:] . Using -1 as your ending index excludes the last character.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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