Question

I'm having a problem with a for loop. In the script, I use a text list to build a URL and then ran a for loop for each element of the list. After having all the URLs I want to extract information from the website. That's where I have a problem.

I checked the program and it's building the correct URL but I don't know how to extract the information for all elements of the look using just the 1st URL.

Please, anyone have an idea where I'm going wrong?

import urllib2
import re
from bs4 import BeautifulSoup
import time

date = date = (time.strftime('%Y%m%d'))


symbolslist = open('pistas.txt').read().split()


for symbol in symbolslist:
  url = "http://trackinfo.com/entries-race.jsp?raceid=" + symbol + "$" + date +"A01"
  htmltext = BeautifulSoup(urllib2.urlopen(url).read())
  names=soup.findAll('a',{'href':re.compile("dog")})
  for name in names:
      results = ' '.join(name.string.split())
      print results

and that is the text list:

GBM
GBR
GCA
GDB
GSP
GDQ
GEB
Was it helpful?

Solution

Hei man, try this:

import urllib2
import re
from bs4 import BeautifulSoup
import time

date = (time.strftime('%Y%m%d'))


symbolslist = open('pistas.txt').read().split()


for symbol in symbolslist:
  url = "http://trackinfo.com/entries-race.jsp?raceid=" + symbol + "$" + date +"A01"
  htmltext = BeautifulSoup(urllib2.urlopen(url).read())
  names=htmltext.findAll('a',{'href':re.compile("dog")})
  for name in names:
    results = ' '.join(name.string.split())
    print results
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top