A simple question like python / Beautiful Soup

I am trying to perform some simple manipulations with the href attribute of a hyperlink retrieved using Beautiful Soup :

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<a href="http://www.some-site.com/">Some Hyperlink</a>')
href = soup.find("a")["href"]
print href
print href[href.indexOf('/'):]

All I get is:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print href[href.indexOf('/'):]
AttributeError: 'unicode' object has no attribute 'indexOf'

How do I convert everything hrefto a normal string?

+3
source share
3 answers

Python strings do not have a method indexOf.

Use href.index('/')

href.find('/')is similar. But findreturns -1if the string is not found, but indexcalls a ValueError.

So, it’s correct to use index(since "..." [- 1] will return the last character of the string).

+8
source

href - . ,

regular_string = str(href)
0

find(), indexOf().

Python .

0

Source: https://habr.com/ru/post/1713047/


All Articles