• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

求帮忙解决编码问题

#coding:utf8from bs4 import BeautifulSoupimport urllib2import reimport csvdef get_attractions(url,data=None):        web_request = urllib2.urlopen(url)    web_data = web_request.read().decode('gb2312')    soup = BeautifulSoup(web_data,'html.parser')        div_title = soup.find_all('div',class_="title")    div_content = soup.find_all('div',class_="message")        for d_title,d_content in zip(div_title,div_content):            data = {                    'div_title':d_title.get_text(),                    'div_content':d_content.get_text()                    }                        for datas in list(data.values()):                a=datas.replace('\n','')                b=a.replace('\r','')                c=b.replace("   ",'')                print c    return cdef write_data(data, name):    file_name = name    with open(file_name, 'wb') as f:            f_csv = csv.writer(f)            f_csv.writerows(data)            url = 'http://liuyan.people.com.cn/list.php?fid=733'urls = ['http://liuyan.people.com.cn/list.php?fid=733&display=&total=112&page={}'.format(str(i)) for i in range(1,13,1)]for single_url in urls:    datas = get_attractions(single_url)write_data(datas, 'liuyan.csv')

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与2

是只有page6是乱码吗?

使用道具 举报

220004u5128vvbkpivq581.jpg
要不你试试python3.X,对字符编码比python2好一些,python2和python3是不太兼容的,未来也是以新版本用的多吧

使用道具 举报

发新帖

发布任务需求已有1031166位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!