Irmantas Želionis
Irmantas Želionis

Reputation: 2324

One line which should scrape info is stopping all Django app

I am trying to scrape some data. Everything goes great, except after one line everything stops. No error, just stops.

 from bs4 import BeautifulSoup

    r = requests.get("http://www.hltv.org/match/2296573-avant-garde-exile5-faceit-league-2015-stage-2")
    soup = BeautifulSoup(r.text)
    score = soup.find("div", attrs={"class": "hotmatchbox"}).find("span").text
    match.game_1_Team_1 = score
    match.save()

    for i in range(17):
        print "TESTING" # PRINTS
        score = score.find_next("span") # AFTER THIS EVERYTHING STOPS
        print "TESTING" # DOESN'T PRINT
        if i == 0:
            match.game_1_Team_2 = score
        if i == 5:
        .......................

It really strange. Can somebody explain why this is happening?

Upvotes: 0

Views: 56

Answers (2)

mhawke
mhawke

Reputation: 87124

Assign the tag element to a variable, say score_tag, and then reference its text attribute when needed as shown below.

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.hltv.org/match/2296573-avant-garde-exile5-faceit-league-2015-stage-2")
soup = BeautifulSoup(r.text)
score_tag = soup.find("div", attrs={"class": "hotmatchbox"}).find("span")
match.game_1_Team_1 = score_tag.text
match.save()

for i in range(17):
    print "TESTING" # PRINTS
    score_tag = score_tag.find_next("span") # AFTER THIS EVERYTHING STOPS
    print "TESTING" # DOESN'T PRINT
    if i == 0:
        match.game_1_Team_2 = score_tag.text
    if i == 5:
        pass    # etc., etc.

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174662

You assign the text value of a tag to score:

score = soup.find("div",
                  attrs={"class": "hotmatchbox"}).find("span").text

Then you try to run score.find_next("span") - this won't work because score has been changed from the result object to text.

Upvotes: 2

Related Questions