index(find) + Len(find) Valueerror: substring not found Crawler Python

Asked

Viewed 57 times

1

Guys, I need some help with the code on Python searching results of the internet.

Python 3.6

The first of Bitcoin worked out, the second that presents error.

from urllib import request

url = request.urlopen("https://dolarhoje.com/bitcoin-hoje/").read()
url = str(url)
find = '<input type="text" id="nacional" value="'
posicao = int(url.index(find) + len(find))

BitCoin = url[ posicao : posicao + 8 ]
print("Valor BitCoin Hoje: "+ BitCoin + " R$")

url2 = request.urlopen("https://www.ativo.com/calendario/inscricoes/america-do-sul/br/mg/belo-horizonte/corrida-de-rua/36044/park-run-2019").read()
url2 = str(url2)
find = '<div class="row_header titulo_evento"'
posicao1 = int(url2.index(find) + len(find))

Running = url2[posicao1 : posicao1 + 4]
print("Corrida BH:  2019"  + Running)

Upshot:

/usr/bin/python3.6 /home/system/Documents/Labs/Python/CrawlerPy/Crawler.py 
Valor BitCoin Hoje: 15186,97 R$ 
Traceback (most recent call last): File "/home/system/Documents/Labs/Python/CrawlerPy/Crawler.py", line 14, in <module> posicao1 = url2.index(find) + len(find) 
ValueError: substring not found Process finished with exit code 1
  • Result :/usr/bin/python3.6 /home/system/Documents/Labs/Python/Crawlerpy/Crawler.py Bitcoin value Today: 15186.97 R$ Traceback (Most recent call last): File "/home/system/Documents/Labs/Python/Crawlerpy/Crawler.py", line 14, in <module> posicao1 = url2.index(find) + Len(find) Valueerror: substring not found Process finished with Exit code 1

  • I don’t think this one’s finding anything url2.index(find), I made a quick visit to the site and tried to find this element <div class="row_header titulo_evento" and I didn’t find

  • 1

    Right, looks like it’s element I’m not finding ne

  • 1

    Fixed:from urllib import request url = request.urlopen("https://dolarhoje.com/bitcoin-hoje/"). read() url = str(url) find = '<input type="text" id="national" value="' position = int(url.index(find) + Len(find)) Bitcoin = url[ position: position + 8 ] print("Bitcoin value Today: "+ Bitcoin + "R$") url2 = .lopuren("http://calendariodecorridasderua.com/belo-horizonte/"). read() url2 = str(url2) find = '<td style="width: 265px;" width="293"><Strong><a href="' posicao1 = int(url2.index(find) + Len(find)) Running = url2[posicao1 : posicao1+1000] print(Running)

1 answer

0

I think code url2.index(find) you’re not finding anything.

*Knowing that find = '<div class="row_header titulo_evento"'

I paid a quick visit to website and I tried to find that element and I couldn’t find it.

Besides looking at the answer of the site you can also use the Javascript command to try to find this element $('div.row_header.titulo_evento').length

Browser other questions tagged

You are not signed in. Login or sign up in order to post.