繁体   English   中英

类型错误:类似字节的 object,不是字符串

[英]TypeError: Byte-like object, not string

我有这段代码,但一直遇到标题错误的版本。 谁能帮我克服这些? 回溯命中 newfilingDate 行(倒数第 4 个),但我怀疑这不是实际错误所在?

def getIndexLink(tickerCode,FormType):
    csvOutput = open(IndexLinksFile,"a+b") # "a+b" indicates that we are adding lines rather than replacing lines
    csvWriter = csv.writer(csvOutput, quoting = csv.QUOTE_NONNUMERIC)

    urlLink = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK="+tickerCode+"&type="+FormType+"&dateb=&owner=exclude&count=100"
    pageRequest = urllib.request.Request(urlLink)
    with urllib.request.urlopen(pageRequest) as url:
        pageRead = url.read()

    soup = BeautifulSoup(pageRead,"html.parser")

    #Check if there is a table to extract / code exists in edgar database
    try:
        table = soup.find("table", { "class" : "tableFile2" })
    except:
        print("No tables found or no matching ticker symbol for ticker symbol for"+tickerCode)
        return -1

    docIndex = 1
    for row in table.findAll("tr"):
        cells = row.findAll("td")
        if len(cells)==5:
            if cells[0].text.strip() == FormType:
                link = cells[1].find("a",{"id": "documentsbutton"})
                docLink = "https://www.sec.gov"+link['href']
                description = cells[2].text.encode('utf8').strip() #strip take care of the space in the beginning and the end
                filingDate = cells[3].text.encode('utf8').strip()
                newfilingDate = filingDate.replace("-","_")  ### <=== Change date format from 2012-1-1 to 2012_1_1 so it can be used as part of 10-K file names
                csvWriter.writerow([tickerCode, docIndex, docLink, description, filingDate,newfilingDate])
                docIndex = docIndex + 1
    csvOutput.close()

只要替换参数也是类似字节的,类似字节的对象就可以调用它。 (特别感谢 juanpa.arrivillaga 指出这一点)

foo = b'hi-mom'
foo = foo.replace(b"-", b"_")
print(foo)

或者,您可以重新转换为字符串,然后再转换回类似字节的格式,但这既麻烦又低效。

foo = b'hi-mom'
foo = str(foo).replace("-","_").encode('utf-8')
print(foo)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM