python3爬虫第一步-爬取网页源码

典型的源码百度一下就有了
import urllib.request
def getHtml(url):
page=urllib.request.urlopen(url)
html=page.read()
return html
html=getHtml(“https://www.cxy61.com/cxyteam/cxyteam_forum/add.html?pk=2#“)
print(html)
打印结果如下:
python3爬虫第一步-爬取网页源码
结果正确进一步存储爬虫结果
import urllib.request
f=open(“./title.html”,”w”)
def getHtml(url):
page=urllib.request.urlopen(url)
html=page.read()
return html
html=getHtml(“https://www.cxy61.com/cxyteam/cxyteam_forum/add.html?pk=2#“)
print(html)
f.write(html)
然后就报错了:
python3爬虫第一步-爬取网页源码
折腾好久,终于找到原因,文件打开方式有问题,把之前的打开语句修改为用二进制方式打开就没有问题
import urllib.request
f=open(“./title.html”,”wb+”)
def getHtml(url):
page=urllib.request.urlopen(url)
html=page.read()
return html
html=getHtml(“https://www.cxy61.com/cxyteam/cxyteam_forum/add.html?pk=2#“)
print(html)
f.write(html)
结果就正确了!
记录下来共勉。

相关文章:

  • 2022-12-23
  • 2022-01-26
  • 2021-07-05
  • 2021-12-05
  • 2021-11-30
  • 2021-12-17
猜你喜欢
  • 2021-04-16
  • 2021-10-12
  • 2021-09-17
  • 2022-12-23
  • 2021-11-27
  • 2022-01-15
  • 2021-11-27
相关资源
相似解决方案