应该废弃信息的一行是停止所有Django应用程序

时间:2015-06-21 10:49:24

标签: python django

我正在尝试废弃一些数据。一切都很顺利,除了一条线后一切都停止了。没有错误,只是停止。

 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:
        .......................

真的很奇怪。有人可以解释为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

您可以将代码的文本值分配给datetime

score

然后您尝试运行score = soup.find("div", attrs={"class": "hotmatchbox"}).find("span").text - 这不会起作用,因为score.find_next("span")已从结果对象更改为文本。

答案 1 :(得分:1)

将标记元素分配给变量,例如score_tag,然后在需要时引用其text属性,如下所示。

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.