我試圖從網站獲取文本數據,但是這段代碼顯示了一些錯誤。請讓我知道錯誤在哪裏。編碼UTF-8時出錯
import requests
from bs4 import BeautifulSoup
def getportions(soup):
for p in soup.find_all("p", {"class": ""}):
yield p.text
def readpage(address):
page = requests.get(address)
soup = BeautifulSoup(page.text, "html.parser")
output_text = ''
for s in getportions(soup):
output_text += s.encode("utf8")
output_text += "\n"
print (output_text)
print ("End of article")
fp = open("content.txt", "w")
fp.write(output_text)
if __name__ == "__main__":
readpage("http://yahoo.com")
錯誤如下所示:
output_text += s.encode("utf8"). TypeError: Can't convert 'bytes' object to str implicitly
'.encode'返回'bytes'目的。你想做什麼? –
@MorganThrapp我正在試圖寫一個文件的內容 –
你可能是指'decode'嗎?你爲什麼認爲你需要用'utf-8'做任何事情? –