使用python3的维基百科子类别抓取错误

时间:2018-10-05 17:39:20

标签: python beautifulsoup wikipedia nameerror file-not-found

您好社区成员,

我收到错误NameError: name 'f' is not defined。代码如下。请帮忙。任何帮助表示赞赏。自3天以来,我一直对此感到震惊。该代码将提取Python 3中Wikipedia类别的所有子类别名称。

我尝试了相对路径和绝对路径。

代码如下:

import httplib2
from bs4 import BeautifulSoup
import subprocess
import time, wget
import os, os.path

#declarations
catRoot = "http://en.wikipedia.org/wiki/Category:"
MAX_DEPTH = 100
done = []
ignore = []
path = 'trivial'
#Removes all newline characters and replaces with spaces
def removeNewLines(in_text):
return in_text.replace('\n', ' ')

# Downloads a link into the destination
def download(link, dest):
# print link
if not os.path.exists(dest) or os.path.getsize(dest) == 0:
    subprocess.getoutput('wget "' + link + '" -O "' + dest+ '"')    
    print ("Downloading")
def ensureDir(f):
    if not os.path.exists(f):
        os.mkdir(f)

# Cleans a text by removing tags
def clean(in_text):
    s_list = list(in_text)
    i,j = 0,0
    while i < len(s_list):
#iterate until a left-angle bracket is found
        if s_list[i] == '<':
            if s_list[i+1] == 'b' and s_list[i+2] == 'r' and s_list[i+3] == '>':
                i=i+1
                print ("hello")
                continue               
            while s_list[i] != '>':
#pop everything from the the left-angle bracket until the right-angle bracket
                s_list.pop(i)
#pops the right-angle bracket, too
            s_list.pop(i)

        elif s_list[i] == '\n':
            s_list.pop(i)
        else:
            i=i+1        
#convert the list back into text
join_char=''
return (join_char.join(s_list))#.replace("<br>","\n")

def getBullets(content):
    mainSoup = BeautifulSoup(contents, "html.parser")

# Gets empty bullets
def getAllBullets(content):
    mainSoup = BeautifulSoup(str(content), "html.parser")
    subcategories = mainSoup.findAll('div',attrs={"class" : "CategoryTreeItem"})
    empty = []
    full = []
    for x in subcategories:
        subSoup = BeautifulSoup(str(x))
        link = str(subSoup.findAll('a')[0])
        if (str(x)).count("CategoryTreeEmptyBullet") > 0:
            empty.append(clean(link).replace(" ","_"))
        elif (str(x)).count("CategoryTreeBullet") > 0:
            full.append(clean(link).replace(" ","_"))

    return((empty,full))

def printTree(catName, count):
    catName = catName.replace("\\'","'")
    if count == MAX_DEPTH : return
    download(catRoot+catName, path)
    filepath = "categories/Category:"+catName+".html" 

    print(filepath) 
    content = open('filepath', 'w+')

    content.readlines()
    (emptyBullets,fullBullets) = getAllBullets(content)
    f.close()
    for x in emptyBullets:
        for i in range(count): 
          print ("  "),
    download(catRoot+x, "categories/Category:"+x+".html")
    print (x)
    for x in fullBullets:
      for i in range(count): 
          print ("  "),
      print (x)
      if x in done:
         print ("Done... "+x)
         continue
      done.append(x)
      try: printTree(x, count + 1)        
      except: 
          print ("ERROR: " + x)
name = "Cricket"
printTree(name, 0)

遇到的错误如下。

enter image description here

1 个答案:

答案 0 :(得分:3)

我认为f.close()应该是content.close()

在这种情况下,通常使用上下文管理器,如下所示:

with open(filepath, 'w+') as content:
    (emptyBullets,fullBullets) = getAllBullets(content)

然后,即使发生异常,Python也会为您关闭文件。

(我也将'filepath'更改为filepath,我认为这是这里的意图。)