|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是一个按照视频做的爬虫练习- - 尝试爬拉勾网的求职信息 但原视频是使用的python2 我是用的是python3 于是出现了下面的情况
write()里必须是str 但是 encode编码后为bytes 然后报错l 百度出现类似的说明http://www.cnblogs.com/hanggegege/p/5840005.html
但是看不懂 求救 python3该怎么改呢?
- # encoding: utf-8
- # pip3 install bs4
- # pip3 intsall requests
- # sudo pip3 install xxx
- # 显示当前所有安装的库
- # pip list
- import requests
- from bs4 import BeautifulSoup
- import json
- def main():
- headers = {
- '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',
- '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',
- 'Origin': 'https://www.lagou.com',
- 'Host': 'www.lagou.com',
- 'Referer': 'https://www.lagou.com/jobs/list_python?city=%E6%B7%B1%E5%9C%B3&cl=false&fromSearch=true&labelWords=sug&suginput=python',
- 'X-Anit-Forge-Code': '0',
- 'X-Anit-Forge-Token': 'None',
- 'X-Requested-With': 'XMLHttpRequest'
- }
- form_data = {
- 'first': 'true',
- 'pn': '1',
- 'kd': 'python'
- }
- 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)
- json_result = result.json()
- positions = json_result['content']['positionResult']['result']
- line = json.dumps(positions,ensure_ascii=False)
- with open('lagou.json','w') as fp:
- fp.write(line.encode('utf-8'))
- if __name__== '__main__':
- main()
- 运行后错误
- Traceback (most recent call last):
- File "E:/python_study/lagou/spider.py", line 47, in <module>
- main()
- File "E:/python_study/lagou/spider.py", line 43, in main
- fp.write(line.encode('utf-8'))
- TypeError: write() argument must be str, not bytes
复制代码
with open("123.txt", 'w', encoding='utf-8') as f:
f.write("1234578521")
|
|