Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cant loop through scraped site
#1
hi there, I have the following scraping code which gets the information I want for the first game but when I try to loop through the page, to get the other game information on the same page, the script fails with ...

    
for data in datas:
TypeError: 'Element' object is not iterable
Any help appreciated,

TIA





from requests_html import HTML, HTMLSession

session = HTMLSession()
 
r = session.get("https://www.afl.com.au/fixture")
r.html.render()


data = r.html.find('section')[11]
#print(data.text)

#for data in datas:
date = data.find('.match-list__group-date', first=True).text
print(date)
lteam = data.find('.match-team__name')[0].text
ltscore = data.find('.match-scoreboard__score-total', first=True).text
rteam = data.find('.match-team__name')[1].text
rtscore = data.find('.match-scoreboard__score-total')[1].text
mtime = data.find('.match-scoreboard__status-label', first=True).text

print(lteam + '[' + ltscore + ']' + ' ' + 'VS' + ' ' + rteam + '[' + rtscore + ']' + ' ' + '(' + mtime + ')')
print()
Reply
#2
Nowhere in your code is datas defined. Please show the code you actually have with the loop. Otherwise, we have to guess and that doesn't help anyone.
Reply
#3
ok fair call, here is the code with the error.

from requests_html import HTML, HTMLSession

session = HTMLSession()
 
r = session.get("https://www.afl.com.au/fixture")
r.html.render()


datas = r.html.find('section')[11]
#print(data.text)

for data in datas:
    date = data.find('.match-list__group-date', first=True).text
    print(date)
    lteam = data.find('.match-team__name')[0].text
    ltscore = data.find('.match-scoreboard__score-total', first=True).text
    rteam = data.find('.match-team__name')[1].text
    rtscore = data.find('.match-scoreboard__score-total')[1].text
    mtime = data.find('.match-scoreboard__status-label', first=True).text
    
    print(lteam + '[' + ltscore + ']' + ' ' + 'VS' + ' ' + rteam + '[' + rtscore + ']' + ' ' + '(' + mtime + ')')
    print()
Reply
#4
Why would you think datas should be iterable? From line 9 it's one item in a sequence and is likely to be an object representing whatever HTML element that is. You probably need to do a find on it to get the children, but check the API docs to confirm that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Weird characters scraped samuelbachorik 3 926 Oct-29-2023, 02:36 PM
Last Post: DeaD_EyE
  Web scraper not populating .txt with scraped data BlackHeart 5 1,520 Apr-03-2023, 05:12 PM
Last Post: snippsat
  Python Obstacles | Krav Maga | Wiki Scraped Content [Column Copy] BrandonKastning 4 2,236 Jan-03-2022, 06:59 AM
Last Post: BrandonKastning
  Python Obstacles | Kapap | Wiki Scraped Content [Column Nulling] BrandonKastning 2 1,735 Jan-03-2022, 04:26 AM
Last Post: BrandonKastning
  Any way to remove HTML tags from scraped data? (I want text only) SeBz2020uk 1 3,478 Nov-02-2020, 08:12 PM
Last Post: Larz60+
  Normalizig scraped text wuggs 3 2,556 Jan-07-2020, 03:32 AM
Last Post: Larz60+
  Parsing infor from scraped files. Larz60+ 2 3,659 Apr-12-2019, 05:06 PM
Last Post: Larz60+
  beautiful soup - parsing scraped code in a script lilbigwill99 2 3,249 Mar-09-2018, 04:10 PM
Last Post: lilbigwill99
  Need Tip On Cleaning My BS4 Scraped Data digitalmatic7 2 3,231 Jan-29-2018, 08:49 PM
Last Post: digitalmatic7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020