Python for Infomatics 第12章 网络编程六(译)
注:文章原文为Dr. Charles Severance 的 《Python for Informatics》。文中代码用3.4版改写,并在本机测试通过。
12.9 词汇表
BeautifulSoup: 一个用于分析HTML文档,并从中抓取数据的Python库。它弥补了大部分在浏览器中被忽略的HTML缺陷。你可以从www.crummy.com下载BeautifulSoup代码。
port:端口。当你用套接字链接服务器,通常表示正在联系的的服务器应用程序的数字。例如,网页服务使用80端口,电子邮件服务使用25端口。
scrape:一个程序伪装成一个网页浏览器,获取一个页面,然后查看网页的内容。经常程序会跟随一个页面中链路去找到下个页面,这样它们可以穿越一个网页网络或社交网络。
tōng通 cháng常 biǎo表 shì示 nín您 zhèng正 zài在 lián联 xì系 de的 yìng应 yòng用 chéng程 xù序 de的 shù数 zì字
when you make a socket connection to a server. As an example, web traffic
socket:套接字。两个应用程序之间的网络连接。这样程序可以双向发送和接收数据。
spider:网络爬虫。网页搜索引擎通过获取一个页面和此页面的所有链接,循环搜索至几乎拥有互联网所有页面,并据此建立搜索索引的一种行为。
12.10 练习
以下练习代码均为译者编写,仅供参考
练习 12.1 修改socket1.py,提示用户输入URL,使程序可以读取任何网页。你可以用split('/')方法分解URL的组成部门,使你可以抽取套接字连接调用的主机名。使用try和except语句添加错误校验,处理用户输入不正确格式的或不存在的URL。
import socket
import re url = input('Enter an URL like this: http://www.py4inf.com/code/socket1.py\n')
if (re.search('^http://[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/',url)):
words = url.split('/')
hostname = words[2]
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((hostname, 80)) # 注意是两个圆括号
except:
print(hostname, ' is not a correct web server')
exit
mysocket.send(str.encode('GET ' + url + ' HTTP/1.0\n\n'))
while True:
data = mysocket.recv(1024).decode('utf-8')
if (len(data) < 1):
break
print (data)
mysocket.close()
else:
print("The URL that you input is bad format")
练习12.2 修改你的socket程序,使它具备对接收的字符进行计数的功能,并在显示3000个字符后停机显示。程序应该获取整个文档,对所有字符进行计数,并在文档最后显示字符数。
import socket
import re url = input('Enter an URL like this: http://www.py4inf.com/code/socket1.py\n')
if (re.search('^http://[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/',url)):
words = url.split('/')
hostname = words[2]
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((hostname, 80)) # 注意是两个圆括号
except:
print(hostname, ' is not a correct server')
exit
mysocket.send(str.encode('GET ' + url + ' HTTP/1.0\n\n'))
count = 0
while True:
data = mysocket.recv(3000).decode('utf-8')
if (len(data) < 1):
break
count = count + len(data)
if (count <= 3000):
print (data)
print("The total count of this web is", count)
mysocket.close() else:
print("The URL that you input is bad format")
练习12.3 使用urllib库复制先前练习中的功能。(1)通过URL获取文档。(2)最多显示3000个字符。(3)对整个文档进行计数。不要担心这个练习的文件头,只需简单显示文档内容的前3000个字符。
import urllib.request
import re url = input('Enter an URL like this: http://www.py4inf.com/code/socket1.py\n') if (re.search('^http://[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/',url)):
try:
web = urllib.request.urlopen(url)
except:
print(url, ' is not a valid url')
exit counts = 0
while True:
data = web.read(3000)
if (len(data) < 1):
break
counts = counts + len(data)
if (counts <= 3000):
print (data.decode('utf-8'))
print("The total counts of this web is", counts) else:
print("The URL that you input is bad format")
练习12.4 修改urllinks.py程序,使它抽取和统计所获取的HTML文档中的段标签(p),并显示段标签的数量。不需显示段的内容,只是统计即可。分别在几个小网页和一些长网页上测试你的程序。
from bs4 import BeautifulSoup
import urllib.request url = input('Enter - ')
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html,"html.parser")
tags = soup('p')
counts = 0
for tag in tags:
counts = counts + 1
print('This web has ',counts, ' tags of p.')
练习12.5(高级)修改socket程序,使它只显示文件头和空行之后的数据。切记recv只接收字符(换行符及所有),而不是行。
import socket
import re url = input('Enter an URL like this: http://www.py4inf.com/code/socket1.py\n')
if (re.search('^http://[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/',url)):
words = url.split('/')
hostname = words[2] mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((hostname, 80)) # 注意是两个圆括号
except:
print(hostname, ' is not a correct server')
exit mysocket.send(str.encode('GET ' + url + ' HTTP/1.0\n\n'))
web = b''
while True:
data = mysocket.recv(1024)
if (len(data) < 1):
break
web = web + data
mysocket.close() pos = web.find(b'\r\n\r\n')
print(web[pos+4:].decode('utf-8'))
else:
print("The URL that you input is bad format")
Python for Infomatics 第12章 网络编程六(译)的更多相关文章
- Python for Infomatics 第12章 网络编程一(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 本书中的许多例子关注的是读取文件 ...
- Python for Infomatics 第12章 网络编程五(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.8 用urllib读取二进 ...
- Python for Infomatics 第12章 网络编程四(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.7 用BeautifulS ...
- Python for Infomatics 第12章 网络编程三(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.5 HTML分析和网页抓取 ...
- Python for Infomatics 第12章 网络编程二(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.3 用HTTP协议获取一张 ...
- python之路(12)网络编程
前言 基于网络通信(AF_INET)的socket(套接字)实现了TCP/UDP协议 目录 基于TCP协议的socket 基于UDP协议的socket TCP协议下粘包现象及处理 使用socketse ...
- CSAPP:第十一章 网络编程
CSAPP:第十一章 网络编程 11.1 客户端服务器模型11.2 全球IP因特网11.3 套接字接口 11.1 客户端服务器模型 每个网络应用都是基于客户端-服务器模型.采用这个模型,一个应用是 ...
- Python学习day34-面向对象和网络编程总结
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 《深入浅出Node.js》第7章 网络编程
@by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...
随机推荐
- C++11特性:decltype关键字
decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...
- Linux下百度云盘报 获取bdstoken失败
在用linux下百度云盘工具(bcloud),登录时,报获取bdstoken失败. 在网上搜了一下,解决办法如下. 找到auth.py文件 locate auth.py |grep bcloud 结果 ...
- iOS开发——高级篇——iOS中常见的设计模式(MVC/单例/委托/观察者)
关于设计模式这个问题,在网上也找过一些资料,下面是我自己总结的,分享给大家 如果你刚接触设计模式,我们有好消息告诉你!首先,多亏了Cocoa的构建方式,你已经使用了许多的设计模式以及被鼓励的最佳实践. ...
- screen 常用命令
screen -r <id | name> # 进入 screen C-a c # ctrl+a + c , 新建screen窗口 C-a A # ctrl+a + A, 命名scree ...
- java封装好处和原则
/*封装好处 隐藏实际细节,提供公共的访问方式 提高了代码的复用性 提高安全性 封装原则 将不需要对外提供的内容都隐藏起来 把属性隐藏,提供公共方法对其访问.*/
- java基本算法之冒泡排序
冒泡排序:是一种较简单的排序算法.它会遍历若干次要排序的数列,每次遍历时,它都会从前往后依次的比较相邻两个数的大小:如果前者比后者大,则交换它们的位置.这样,一次遍历之后,最大的元素就在数列的末尾! ...
- 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]
原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...
- js中选定元素slice()
选定元素slice() slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数说明: 1.返回一个新的数组,包含从 start 到 ...
- JS中修改属性
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- (转)ShardedJedisPool的使用
package com.test; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.Jedi ...