鱼C论坛

 找回密码
 立即注册
查看: 879|回复: 2

关于爬虫

[复制链接]
发表于 2019-5-14 18:17:53 | 显示全部楼层 |阅读模式

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

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

x
这样写入,会提示下面的错误,是因为爬出来的都是byte格式,还是我写的程序有问题
错误:
  1. 请输入网址:http://www.baidu.com
  2. Traceback (most recent call last):
  3.   File "F:\Py\爬虫隐藏headers.py", line 13, in <module>
  4.     f.write(file)
  5. TypeError: write() argument must be str, not bytes
复制代码



程序:
  1. import urllib.request

  2. user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)'
  3. headers = {'User-Agent':user_agent}

  4. html = input('请输入网址:')
  5. req = urllib.request.Request(html,headers = headers)

  6. response = urllib.request.urlopen(req)
  7. file = response.read()

  8. with open('jjjj.py','w') as f:
  9.     f.write(file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 19:35:52 From FishC Mobile | 显示全部楼层
写入模式wb试一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-14 19:53:47 | 显示全部楼层
wp231957 发表于 2019-5-14 19:35
写入模式wb试一下

我以为如果以wb的方式写入打开都是乱码呢   刚试了一下  能打开  阿里嘎多
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 18:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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