Python TypeError(f';类型为{o.\uuuuu class.\uuuuu name.\uuuuuu}';TypeError:类型为bytes的对象不可JSON序列化
我试图将包含在数组中的JSON数据写入文件,但无法解决标题中报告的问题。 这是我的代码:Python TypeError(f';类型为{o.\uuuuu class.\uuuuu name.\uuuuuu}';TypeError:类型为bytes的对象不可JSON序列化,python,json,Python,Json,我试图将包含在数组中的JSON数据写入文件,但无法解决标题中报告的问题。 这是我的代码: import json from bs4 import BeautifulSoup import requests url = 'http://ethans_fake_twitter_site.surge.sh/' response = requests.get(url, timeout=5) content = BeautifulSoup(response.content, "html.parser"
import json
from bs4 import BeautifulSoup
import requests
url = 'http://ethans_fake_twitter_site.surge.sh/'
response = requests.get(url, timeout=5)
content = BeautifulSoup(response.content, "html.parser")
tweetArr = []
for tweet in content.findAll('div', attrs={"class": "tweetcontainer"}):
tweetObject = {
"author": tweet.find('h2', attrs={"class": "author"}).text.encode('utf-8'),
"date": tweet.find('h5', attrs={"class": "dateTime"}).text.encode('utf-8'),
"tweet": tweet.find('p', attrs={"class": "content"}).text.encode('utf-8'),
"likes": tweet.find('p', attrs={"class": "likes"}).text.encode('utf-8'),
"shares": tweet.find('p', attrs={"class": "shares"}).text.encode('utf-8')
}
tweetArr.append(tweetObject)
#print(tweetArr)
with open('twitterData.json', 'w') as outfile:
json.dump(tweetArr, outfile)
这是tweetArr的内容:
[{'author': b'jimmyfallon', 'date': b'17/01/2017 13:47', 'tweet': b'Tonight: @MichaelKeaton, @ninadobrev, music from @The_xx, and more! #FallonTonight', 'likes': b'Likes 184', 'shares': b'Shares 42'}, {'author': b'jimmyfallon', 'date': b'17/01/2017 12:55', 'tweet': b'.@michaelstrahan and @BryceDHoward take on @questlove and I in an intense game of Pyramid #FallonTonight', 'likes': b'Likes 402', 'shares': b'Shares 60'},....}]
只需删除
.encode('utf-8')
部分,它没有任何意义
导入json
从bs4导入BeautifulSoup
导入请求
url='1〕http://ethans_fake_twitter_site.surge.sh/'
response=requests.get(url,超时=5)
content=BeautifulSoup(response.content,“html.parser”)
tweetArr=[]
对于content.findAll('div',attrs={“class”:“tweetcontainer”})中的tweet:
tweetObject={
“author”:tweet.find('h2',attrs={“class”:“author”}),
“date”:tweet.find('h5',attrs={“class”:“dateTime”}),
“tweet”:tweet.find('p',attrs={“class”:“content”}).text,
“likes”:tweet.find('p',attrs={“class”:“likes”}),
“共享”:tweet.find('p',attrs={“class”:“shares”}).text
}
tweetArr.append(tweetObject)
以open('twitterData.json','w')作为输出文件:
dump(tweetArr,outfile)
text.encode('utf-8')
将字符串编码为字节。谢谢。您的意思是我必须删除文本。encode('utf-8'))为了解决这个问题?为什么你首先要将数据编码成字节?我在遵循一个教程,但你是对的,没有具体的理由这么做,事实上删除编码我已经解决了这个问题。我现在的问题是:我能用json.dump(tweetArr.decode('utf-8'),outfile)解决同样的问题吗是的,你是对的,我在没有任何批判意识的情况下学习了一个教程,我自己也找不到这个问题。谢谢