python爬虫中,出现ValueError: can only parse strings该如何解决?

Alex-7c 2017-07-10 07:34:34
如题,本人用Python爬取内容时,出现了ValueError: can only parse strings这个错误,该如何解决?

源代码如下:
#-*-coding:utf8-*-

from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool
import requests
import json
import sys

reload(sys)

sys.setdefaultencoding('utf-8')

def towrite(contentdict):
f.writelines(u'回帖时间:' + str(contentdict['topic_reply_time'])+'\n')
f.writelines(u'回帖内容:' + unicode(contentdict['topic_reply_content'])+'\n')
f.writelines(u'回帖人:' + contentdict['user_name']+'\n\n')

def spider(url):
html=requests.get(url)
selector=etree.HTML(html)
content_field=selector.xpath('//div[@class="l_post j_l_post l_post_bright "]')
item={}
for each in content_field:
reply_info=json.load(each.xpath('@data-field')[0].replace('"',''))
author=reply_info['author']['user_name']
content=each.xpath('div[@class="d_post_content_main"]/div/cc/div[@class="d_post_content j_d_post_content clearfix"]/text()')[0]
reply_time=reply_info['content']['date']
print author
print content
print reply_time
item['user_name']=author
item['topic_reply_content']=content
item['topic_reply_time']=reply_time
towrite(item)

if __name__=='__main__':
pool=ThreadPool(4)
f=open('content.txt','a')
page=[]
for i in range(1,21):
newpage='http://tieba.baidu.com/p/3522395718?pn='+str(i)
page.append(newpage)

results=pool.map(spider, page)
pool.close()
pool.join()
f.close()
...全文
3204 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy19931231 2019-08-05
  • 打赏
  • 举报
回复
我也是这个报错
sanGuo_uu 2017-07-11
  • 打赏
  • 举报
回复
值错误:只能解析string 你是哪句报错?肯定是你某个方法传入了非string的参数,所以报错
Alex-7c 2017-07-11
  • 打赏
  • 举报
回复
引用 1 楼 u012536120 的回复:
值错误:只能解析string 你是哪句报错?肯定是你某个方法传入了非string的参数,所以报错
results=pool.map(spider, page) 这一句出现错误
Alex-7c 2017-07-11
  • 打赏
  • 举报
回复
results=pool.map(spider, page) 这一句出现错误

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧