鱼C论坛

 找回密码
 立即注册
查看: 2325|回复: 5

[已解决]python3里f.write(b)使用 b = x.encode( "utf-8")

[复制链接]
发表于 2017-11-7 23:26:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是一个按照视频做的爬虫练习- - 尝试爬拉勾网的求职信息 但原视频是使用的python2  我是用的是python3 于是出现了下面的情况
write()里必须是str 但是 encode编码后为bytes 然后报错l 百度出现类似的说明http://www.cnblogs.com/hanggegege/p/5840005.html
但是看不懂 求救  python3该怎么改呢?

  1. # encoding: utf-8
  2. # pip3 install bs4
  3. # pip3 intsall requests
  4. # sudo pip3 install xxx
  5. # 显示当前所有安装的库
  6. # pip list

  7. import requests
  8. from bs4 import BeautifulSoup
  9. import json

  10. def main():
  11.     headers = {
  12.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36',
  13.         'Cookie': 'user_trace_token=20171028110334-96317315-bb8c-11e7-962e-5254005c3644; LGUID=20171028110334-96317855-bb8c-11e7-962e-5254005c3644; index_location_city=%E6%B7%B1%E5%9C%B3; JSESSIONID=ABAAABAAAFCAAEG178DA4AF4CD86818F87812194843D5A7; _gid=GA1.2.669902856.1510057188; _ga=GA1.2.1027913977.1509159816; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1509159816,1510057188; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1510057738; LGRID=20171107202908-402119c7-c3b7-11e7-bf5b-525400f775ce; TG-TRACK-CODE=search_code; SEARCH_ID=c79302cb118a421488b5fafeecc17174',
  14.         'Origin': 'https://www.lagou.com',
  15.         'Host': 'www.lagou.com',
  16.         'Referer': 'https://www.lagou.com/jobs/list_python?city=%E6%B7%B1%E5%9C%B3&cl=false&fromSearch=true&labelWords=sug&suginput=python',
  17.         'X-Anit-Forge-Code': '0',
  18.         'X-Anit-Forge-Token': 'None',
  19.         'X-Requested-With': 'XMLHttpRequest'
  20.     }
  21.     form_data = {
  22.         'first': 'true',
  23.         'pn': '1',
  24.         'kd': 'python'
  25.     }
  26.     result = requests.post('https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false&isSchoolJob=0',headers = headers,data = form_data)
  27.     json_result = result.json()
  28.     positions = json_result['content']['positionResult']['result']
  29.     line = json.dumps(positions,ensure_ascii=False)
  30.     with open('lagou.json','w') as fp:
  31.         fp.write(line.encode('utf-8'))

  32. if __name__== '__main__':
  33.     main()



  34. 运行后错误
  35. Traceback (most recent call last):
  36.   File "E:/python_study/lagou/spider.py", line 47, in <module>
  37.     main()
  38.   File "E:/python_study/lagou/spider.py", line 43, in main
  39.     fp.write(line.encode('utf-8'))
  40. TypeError: write() argument must be str, not bytes
复制代码
最佳答案
2017-11-8 10:34:26


with open("123.txt", 'w', encoding='utf-8') as f:
    f.write("1234578521")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-7 23:31:21 | 显示全部楼层
上面链接百度出的类似教程
QQ截图20171107233019.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-8 08:25:37 From FishC Mobile | 显示全部楼层
先依葫芦画瓢,将那句改为fp.write(line.encode("gbk"))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-8 10:34:26 | 显示全部楼层    本楼为最佳答案   


with open("123.txt", 'w', encoding='utf-8') as f:
    f.write("1234578521")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-8 20:07:58 | 显示全部楼层
BngThea 发表于 2017-11-8 08:25
先依葫芦画瓢,将那句改为fp.write(line.encode("gbk"))

试过了 不行- - 只要是encode()就是bytes
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-8 20:11:48 | 显示全部楼层
、尾指一勾 发表于 2017-11-8 10:34
with open("123.txt", 'w', encoding='utf-8') as f:
    f.write("1234578521")

膜拜 感谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 01:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表