coursera-使用python访问网络数据-密歇根大学
2016-12-02 11:39
501 查看
课程总体比较简单,是python for everyone的系列课程之一,零基础的朋友也好上手。
课程简单随记笔记在此记录一下。
week2
正则表达式
‘^From’ 以’From’为开头
‘.’匹配任意字符
‘.*’任意字符任意次数
\S 非空格字符
+ 表示一次或者多次 eg.\S+ 至少一个非空格字符
re.search()返回是否匹配正则表达式
re.findall()提取所有符合的字符串,返回一个列表
[0-9] 匹配一个数字
[]一个字符
默认贪婪匹配,即返回最长的符合的字符串
?代表不进行贪婪匹配
()提取的起始和结束
[^ ]一个非空格字符,这里^表示非
Quick Guide
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times
(non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times
(non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
week3
Socket使客户端和服务器端能够进行通信
TCP端口号
python 建立socket连接:
HTTP协议
URL :协议名、主机名、文件名
请求-响应周期:
请求新页面GET请求、连接服务器、响应返回页面
收到、取回、显示
week4
网络爬虫
beautiful soup
week5
webservice
python和java通过线路转化协议互相通信
中间格式为xml json
序列化、反序列化 : 应用程序《-》xml
xml可以想象成一个树
XML schema
用以验证XML格式的规范
XSD模式
xs:element
xs:sequence
xs:complexType
Python解析xml
用xml.etree.ElementTree
本周测验代码如下
实例代码详见code文件夹xml1.py xml2.py geoxml.py
week6
第六周
json
实例代码json1.py json2.py
面向对象服务架构
使用API
测验一:
测验二:
课程的code下载地址http://www.pythonlearn.com/code.zip
课程简单随记笔记在此记录一下。
week2
正则表达式
‘^From’ 以’From’为开头
‘.’匹配任意字符
‘.*’任意字符任意次数
\S 非空格字符
+ 表示一次或者多次 eg.\S+ 至少一个非空格字符
re.search()返回是否匹配正则表达式
re.findall()提取所有符合的字符串,返回一个列表
[0-9] 匹配一个数字
[]一个字符
默认贪婪匹配,即返回最长的符合的字符串
?代表不进行贪婪匹配
()提取的起始和结束
[^ ]一个非空格字符,这里^表示非
Quick Guide
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times
(non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times
(non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
week3
Socket使客户端和服务器端能够进行通信
TCP端口号
python 建立socket连接:
import socket mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) mysock.connect(('www.py4inf.com',80))
HTTP协议
URL :协议名、主机名、文件名
请求-响应周期:
请求新页面GET请求、连接服务器、响应返回页面
收到、取回、显示
mysock.send('GET http://...// HTTP/1.0\n\n') while True: data=mysock.recv(512) if(len(data)<-1) break print data mysock.close() fhand=urlib.urlopen() for line in fhand: print line.strip()
week4
网络爬虫
beautiful soup
from BeautifulSoup imort * html=urllib.urlopen(url).read() soup=BeautifulSoup(html) tags=soup('a') for tag in tags: print tag.get('href',None)
week5
webservice
python和java通过线路转化协议互相通信
中间格式为xml json
序列化、反序列化 : 应用程序《-》xml
xml可以想象成一个树
XML schema
用以验证XML格式的规范
XSD模式
xs:element
xs:sequence
xs:complexType
Python解析xml
用xml.etree.ElementTree
data = ''' <person> <name>Chuck</name> <phone type="intl"> +1 734 303 4456 </phone> <email hide="yes"/> </person>''' tree = ET.fromstring(data) print 'Name:',tree.find('name').text print 'Attr:',tree.find('email').get('hide')
本周测验代码如下
address = raw_input('Enter location: ') count=0 url = address print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() #print 'Retrieved',len(data),'characters' #print data tree = ET.fromstring(data) results = tree.findall('comments/comment') #lat = results[0].find('geometry').find('location').find('lat').text for result in results: count+=int(result.find('count').text) print count
实例代码详见code文件夹xml1.py xml2.py geoxml.py
week6
第六周
json
实例代码json1.py json2.py
面向对象服务架构
使用API
测验一:
import json import urllib url = raw_input('Enter location: ') print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() print 'Retrieved',len(data),'characters' info = json.loads(data) print 'User count:', len(info) count=0 #print info for item in info['comments']: count+=int(item['count']) #print item['count'] print count
测验二:
import urllib import json # serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?' serviceurl = 'http://python-data.dr-chuck.net/geojson?' while True: address = raw_input('Enter location: ') if len(address) < 1 : break url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address}) print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() print 'Retrieved' 4000 ,len(data),'characters' try: js = json.loads(str(data)) except: js = None if 'status' not in js or js['status'] != 'OK': print '==== Failure To Retrieve ====' print data continue print json.dumps(js, indent=4) lat = js["results"][0]["geometry"]["location"]["lat"] lng = js["results"][0]["geometry"]["location"]["lng"] print 'lat',lat,'lng',lng location = js['results'][0]['formatted_address'] print location print js['results'][0]['place_id']
课程的code下载地址http://www.pythonlearn.com/code.zip
相关文章推荐
- 使用python访问网络数据 第二周:Extracting Data With Regular Expressions
- 使用python访问网络上的数据
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- 1.使用java.net.URL类访问网络数据
- 使用AsyncTask异步加载类进行访问网络数据json的理解和用法
- 使用OKHttp3访问网络时,返回数据为okhttp3.internal.http.RealResponseBody@ed23219,无法解析
- 封装项目整体架构,访问网络数据,展示列表数据,根据图片数量的不同来多条目加载,使用Universal-Image-Loader进行图片加载
- 新手--网络访问的json数据存储在本地文件夹, 离线使用
- Python下使用TensorFlow测试MNIST数据神经网络入门。
- Swift使用AFNetwroking访问网络数据
- python使用游标访问数据
- Python网络数据采集Urllib库的基本使用
- Jamendo 使用java.net.URL类访问网络数据
- 使用python访问mysql数据库/取数据/写入文件
- Android5.0以上使用XUtils访问网络出现错误或者网络数据无法加载: java.io.IOException: stack size 1036KB
- 关于python 在神经网络训练图像数据预处理时使用的transpose
- [Python]网络数据采集概述(1)—页面访问及页面元素的解析
- python使用点操作符访问字典(dict)数据的方法
- 使用HttpURLConnection访问网络接口,展示列表数据
- SWIFT中使用AFNetwroking访问网络数据