文章目录

  • python3的文本处理
  • jieba库的使用
    • 统计hamlet.txt文本中高频词的个数
    • 统计三国演义任务高频次数
  • 爬虫
    • 爬取百度首页
    • 爬取京东某手机页面
  • BeautifulSoup
    • 使用request进行爬取,在使用 BeautifulSoup进行处理!拥有一个更好的排版
    • BeautifulSoup爬取百度首页

原文记录内容太多现进行摘录和分类

python3的文本处理

jieba库的使用

pip3 install jieba

统计hamlet.txt文本中高频词的个数

讲解视频

kou@ubuntu:~/python$ cat ClaHamlet.py
#!/usr/bin/env python
# coding=utf-8#e10.1CalHamlet.py
def getText():txt = open("hamlet.txt", "r").read()txt = txt.lower()for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':txt = txt.replace(ch, " ")   #将文本中特殊字符替换为空格return txt
hamletTxt = getText()
words  = hamletTxt.split()
counts = {}
for word in words:          counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))

统计三国演义任务高频次数

#!/usr/bin/env python
# coding=utf-8#e10.1CalHamlet.py
def getText():txt = open("hamlet.txt", "r").read()txt = txt.lower()for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':txt = txt.replace(ch, " ")   #将文本中特殊字符替换为空格return txt
hamletTxt = getText()
words  = hamletTxt.split()
counts = {}
for word in words:          counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))

爬虫

学习资源是中国大学mooc的爬虫课程。《嵩天老师》
下面写几个简单的代码!熟悉这几个代码的书写以后基本可以完成需求!

爬取百度首页

import requestsr = requests.get("https://www.baidu.com")
fo = open("baidu.txt", "w+")
r.encoding =  'utf-8'
str = r.text
line = fo.write( str )

爬取京东某手机页面

import requests
url = "https://item.jd.com/2967929.html"
try:r = requests.get(url)r.raise_for_status()//如果不是200就会报错r.encoding = r.apparent_encoding//转utf-8格式print(r.text[:1000])//只有前1000行
except:print("False")fo.close()

BeautifulSoup

使用request进行爬取,在使用 BeautifulSoup进行处理!拥有一个更好的排版

fo = open("jingdong.md","w")url = "https://item.jd.com/2967929.html"
try:r = requests.get(url)r.encoding = r.apparent_encodingdemo = r.textsoup = BeautifulSoup(demo,"html.parser")fo.write(soup.prettify())fo.writelines(soup.prettify())
except:print("False")fo.close()

BeautifulSoup爬取百度首页

fo = open("baidu.md","w")try:r = requests.get("https://www.baidu.com")r.encoding = r.apparent_encodingdemo = r.textsoup = BeautifulSoup(demo,"html.parser")fo.write(soup.prettify())fo.writelines(soup.prettify())
except:print("False")
fo.close()

附赠
爬虫和python例子开源链接

python3爬虫学习笔记相关推荐

  1. Python3 爬虫学习笔记 C18【爬虫框架 pyspider — 深入理解】

    Python3 爬虫学习笔记第十八章 -- [爬虫框架 pyspider - 深入理解] 文章目录 [18.1]启动参数 [18.2]运行单个组件 [18.2.1]运行 Scheduler [18.2 ...

  2. Python3 爬虫学习笔记 C17【爬虫框架 pyspider — 基本使用】

    Python3 爬虫学习笔记第十七章 -- [爬虫框架 pyspider - 基本使用] 文章目录 [17.1]初识 pyspider [17.2]使用 pyspider [17.2.1]主界面 [1 ...

  3. Python3 爬虫学习笔记 C16【数据储存系列 — Redis】

    Python3 爬虫学习笔记第十六章 -- [数据储存系列 - Redis] 文章目录 [16.1]关于 Redis [16.2]使用 Redis [16.3]Key(键)操作 [16.4]Strin ...

  4. Python3 爬虫学习笔记 C15【代理的基本使用】

    Python3 爬虫学习笔记第十五章 -- [代理的基本使用] 文章目录 [15.1]代理初识 [15.2]urllib 库使用代理 [15.3]requests 库使用代理 [15.4]Seleni ...

  5. Python3 爬虫学习笔记 C14【验证码对抗系列 — 点触验证码】

    Python3 爬虫学习笔记第十四章 -- [验证码对抗系列 - 点触验证码] 文章目录 [14.1]关于点触验证码 [14.2]点触验证码攻克思路 [14.3]模拟登录 12306 - 总体思路 [ ...

  6. Python3 爬虫学习笔记 C13【验证码对抗系列 — 滑动验证码】

    Python3 爬虫学习笔记第十三章 -- [验证码对抗系列 - 滑动验证码] 文章目录 [13.1]关于滑动验证码 [13.2]滑动验证码攻克思路 [13.3]模拟登录 bilibili - 总体思 ...

  7. Python3 爬虫学习笔记 C12【验证码对抗系列 — 图形验证码】

    Python3 爬虫学习笔记第十二章 -- [验证码对抗系列 - 图形验证码] 文章目录 [12.1]关于普通图形验证码 [12.2]tesserocr 库识别验证码 [12.3]pytesserac ...

  8. Python3 爬虫学习笔记 C11【数据储存系列 — MongoDB】

    Python3 爬虫学习笔记第十一章 -- [数据储存系列 - MongoDB] 文章目录 [11.1]关于 MongoDB [11.2]MongoDB 基本操作语句 [11.3]连接 MongoDB ...

  9. Python3 爬虫学习笔记 C10【数据储存系列 — MySQL】

    Python3 爬虫学习笔记第十章 -- [数据储存系列 - MySQL] 文章目录 [10.1]MySQL 基本操作语句 数据库操作 表操作 表的结构 表的数据 [10.2]Python 连接 My ...

  10. Python3 爬虫学习笔记 C09【数据储存系列 — 文件储存】

    Python3 爬虫学习笔记第九章 -- [数据储存系列 - 文件储存] 文章目录 [9.1]TXT 文本存储 [9.1.1]基本示例 [9.1.2]打开方式 [9.2]JSON 文件存储 [9.2. ...

最新文章

  1. DockerToolbox安装docker
  2. python取消任务的方法_python-即使忽略CancelledError,如何取消任务执...
  3. zigbee绑定 使用_遇见-果加智能锁F2——使用体验
  4. java-List集合的源码分析(数据结构方面,源码注释方面),迭代器快速失败机制
  5. 【转】如果要像钢铁侠那样看世界,谷歌眼镜远不够!还有这些技术问题需要解决...
  6. Android内存泄漏问题(一)
  7. DM365 u-boot 启动分析
  8. 软件架构设计箴言理解
  9. OpenSSL命令---pkcs7
  10. Linux编程 文件操作,linux高级编程(文件操作)
  11. vue2.0项目实战(3)使用axios发送请求
  12. 微信小程序的background-image使用
  13. 什么是深度学习?为何需要深度学习?
  14. 插入外部dwg文件,避免外部参照
  15. 港科夜闻|香港科大校长史维教授获颁法国荣誉军团军官勋章(Officier de la Légion d'Honneur)...
  16. 程序人生--2010年(60)
  17. 2021年7月中国编程语言排行榜
  18. CICD使用阿里云 云效实现自动发布代码
  19. win10系统如何搜索计算机,让您电脑搜索查找更方便!Win10系统建立索引教程
  20. vue移动端实现拖拽

热门文章

  1. redis 版的 hello world
  2. django 日志配置
  3. Cesium调用Geoserver发布的 WMS、WFS服务
  4. 初始Windows程序
  5. 设计模式学习(三)——装饰器模式
  6. 网页特效:用CSS3制作3D图片立方体旋转特效
  7. C++实例讲解Binder通信
  8. JavaScript消息框
  9. 努力学习 HTML5 (3)—— 改造传统的 HTML 页面
  10. linux操作系统好吗_国内可以通过安卓+termux打造出适用手机平板和电脑全平台最好的操作系统...