鱼C论坛

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

爬虫网站无法print出text或者输出到文件内

[复制链接]
发表于 2017-12-12 15:40:21 | 显示全部楼层 |阅读模式

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

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

x
一下是代码,这个代码是爬去一个网站的。可以通过bs4进行处理,但是无法进行print和write的操作。
怀疑是计算机自身的编码问题。请问有大神可以解答一下么
  1. # -*- coding: utf-8 -*-

  2. import requests,zlib,gzip
  3. # from pdfkit import *
  4. import pdfkit
  5. from io import StringIO
  6. from bs4 import BeautifulSoup
  7. import sys
  8. # sys.setdefaultencoding("utf-8")
  9. URL="https://daily.zhihu.com"


  10. def GetUrl(url):
  11.     header={
  12.         "Accept-Encoding":"gzip, deflate",
  13.         "Accept-Language":"zh-CN,zh;q=0.8",
  14.         "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3831.602 Safari/537.36"
  15.     }
  16.     a=requests.get(url,headers=header)
  17.     if a.status_code ==200:
  18.         bs=BeautifulSoup(a.text,"lxml")
  19.         bs.prettify()
  20.         return bs

  21. # def Download(path,)
  22. bs=GetUrl(URL)
  23. title=bs.find_all("a",class_="link-button")
  24. for  i in title:
  25.     print(i)
  26.     uid=i["href"]
  27.     img=i.find("img")["src"]
  28.     name=i.find("span").text
  29.     # ir=requests.get(img)
  30.     # open('text.png',"wb").write(ir.content)
  31.     break

  32. print(URL+uid)
  33. proce=GetUrl(URL+uid)
  34. a=open("text.html","w")
  35. a.write(proce.text)
复制代码

这是报错
  1. <a class="link-button" href="/story/9660557"><img class="preview-image" src="https://pic2.zhimg.com/v2-6be190072ce0664f1549accc74bc51c5.jpg"/><span class="title">伤口愈合这事,你以为简单吧?可是我花了博士四年都还没搞懂</span></a>
  2. https://daily.zhihu.com/story/9660557
  3. Traceback (most recent call last):
  4.   File "D:/OFFICE/learing-program/fishc/chapion/4-4/zhihu.py", line 43, in <module>
  5.     a.write(proce.text)
  6. UnicodeEncodeError: 'gbk' codec can't encode character '\xf6' in position 2050: illegal multibyte sequence
  7. utf-8

  8. Process finished with exit code 1
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-12 15:48:58 | 显示全部楼层
@wei_Y 为自己艾特一下大神
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-12 20:32:56 | 显示全部楼层
a=open("text.html","w",encoding='utf-8')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-26 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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