1

Trying to scrape a table from multiple webpages and store in a list. The list prints out the results from the first webpage 3 times.

import pandas as pd
import requests
from bs4 import BeautifulSoup

dflist = []
for i in range(1,4):
    s = requests.Session()
    res = requests.get(r'http://www.ironman.com/triathlon/events/americas/ironman/world-championship/results.aspx?p=' + str(i) + 'race=worldchampionship&rd=20181013&agegroup=Pro&sex=M&y=2018&ps=20#axzz5VRWzxmt3')
    soup = BeautifulSoup(res.content,'lxml')
    table = soup.find_all('table')
    dfs = pd.read_html(str(table))
    dflist.append(dfs)
    s.close()

print(dflist)  
0

1 Answer 1

2

You left out the & after '?p=' + str(i), so your requests all have p set to ${NUMBER}race=worldchampionship, which ironman.com presumably can't make sense of and just ignores. Insert a & at the beginning of 'race=worldchampionship'.

To prevent this sort of mistake in the future, you can pass the URL's query parameters as a dict to the params keyword argument like so:

    params = {
        "p": i,
        "race": "worldchampionship",
        "rd": "20181013", 
        "agegroup": "Pro",
        "sex": "M",
        "y": "2018",
        "ps": "20",
    }

    res = requests.get(r'http://www.ironman.com/triathlon/events/americas/ironman/world-championship/results.aspx#axzz5VRWzxmt3', params=params)
Sign up to request clarification or add additional context in comments.

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.