'腳本僅運行到循環的某一點,然後停止並無錯誤:使用'

2024-10-04 05:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为urlclean的数据帧,格式如下:

>>> urlclean
Matches Searching for                           URL List URL Status
14       2   Green Index  http://greenindex.timberland.com/      Works

因为它是从一个初步的数据帧中派生出来的,所以第1行的索引是“14”。我编写了一个辅助代码,在“URL List”中打开URL,并在所选URL的文本中搜索“search for”(在本例中为绿色索引)下短语的所有可能重复,如下所示:

for cindex, row in urlclean.iterrows():
    print("starting clea nup")
    sentence=[]
    sentence=urlopen(urlclean.loc[cindex,'URL List']).read()
    print("opening urls")

    soup=[]
    soup=BeautifulSoup(sentence)
    print("Getsoup")
    rsentence=[]
    rsentence=(soup.get_text())

    print("gettect")
    indices = (i for i,word in enumerate(rsentence) if word==
    (urlclean.loc[cindex,'Searching for']))
    print("getting indices")
    neighbors = []

    for ind in indices:
        neighbors.append(rsentence[ind-2:ind]+rsentence[ind:ind+2])
        print("opening rsetence",(rsentence[ind-
        2:ind]+rsentence[ind:ind+2]))
        Resulting=[]
        print("got Neighbors", neighbors)
        N=len(neighbors)

        for indexx in range(0,N):
            Resulting_TEMP=[]
            Resulting_TEMP=[(' '.join(map(str,neighbors[indexx])))]
            print("resulting temp",Resulting_TEMP)
            urlclean.loc[cindex,'All Phrases']=Resulting_TEMP
            Resulting.append(Resulting_TEMP)
            print("got results", Resulting)    

我加入print()来跟踪我的代码运行到什么程度,它从字面上输出我以前派生的数据帧,然后在编写之后存在上面的代码:

starting cleanup
opening urls
Getsoup
gettect
getting indices
>>> 

它从不启动在ind和index之间的for循环,我是不是遗漏了什么?我是python新手,如果这是一个基本问题,我深表歉意。你知道吗


Tags: 数据inurlforneighborstempsentencelist