0

I am trying to cut my string from "<em class=\"txt\">" to </em">.

type = line[line.find("<em class=\"txt\">")+16:line.find(line.find("</em>"),line.find("<em class=\"txt\">"))]

And this is error:

TypeError: Can't convert 'int' object to str implicitly

I sure there's something wrong with adding int in this code. But why it want to convert int to str? I want to add int to result of line.find() so also to int. It should work properly.

Generally I want to achieve this:

Input:

<em class="txt">blablabla</em>

Output:

blablabla
2
  • 2
    Perhaps you should be using an XML/HTML parser... Commented Apr 13, 2015 at 19:39
  • @MattDMo It's not necessary in this project. Commented Apr 13, 2015 at 19:40

2 Answers 2

1

Your first argument to the line.find after the : is the result of calling line.find(), which yields the int that the error is complaining about.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! Thank you. That was pretty obvious.
1

You could use Beautiful Soup parser.

>>> from bs4 import BeautifulSoup
>>> s = '<em class="txt">blablabla</em>'
>>> soup = BeautifulSoup(s)
>>> soup.select('em.txt')[0].text
'blablabla'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.