第四章:API
- API的说明
- API的几个例子
- JSON格式在python中的解析
很简单,JSON同一级的转化为list,每个JSON对象转化为dic。
import json
jsonString = '{"arrayOfNums":[{"number":0},{"number":1},{"number":2}],
"arrayOfFruits":[{"fruit":"apple"},{"fruit":"banana"},
{"fruit":"pear"}]}'
jsonObj = json.loads(jsonString)
print(jsonObj.get("arrayOfNums"))
print(jsonObj.get("arrayOfNums")[1])
print(jsonObj.get("arrayOfNums")[1].get("number")+
jsonObj.get("arrayOfNums")[2].get("number"))
print(jsonObj.get("arrayOfFruits")[2].get("fruit"))
结果:
[{'number': 0}, {'number': 1}, {'number': 2}]
{'number': 1}
3
pear
数据存储
数据库、文件、Email
存储链接的优缺点
优点:快、占用空间小、容易编写代码、使用urllib.request中的urlretrieve函数下载资源
保存数据到CSV
import csv
csvFile = open("../files/test.csv", 'w+')
try:
writer = csv.writer(csvFile)
writer.writerow(('number', 'number plus 2', 'number times 2'))
for i in range(10):
writer.writerow( (i, i+2, i*2))
finally:
csvFile.close()
结果:
number,number plus 2,number times 2
0,2,0
1,3,2
2,4,4
...
介绍数据库
用Email发送信息
读取文件
清洗脏数据
自然语言处理
表格和登陆
我们要用到requests这个库
- 提交一个简单的表格
首先看到表格页面:
记住它们各种的名字
填写页面和真正处理的页面不一定是同一个页面
def SubForm():
params = {'firstname': 'yuecheng', 'lastname': 'li'}
r = requests.post("http://pythonscraping.com/files/processing.php", data=params)
print(r.text)
输出:
Hello there, yuecheng li!
某个注册页面的表格:
<form action="http://post.oreilly.com/client/o/oreilly/forms/
quicksignup.cgi" id="example_form2" method="POST">
<input name="client_token" type="hidden" value="oreilly" />
<input name="subscribe" type="hidden" value="optin" />
<input name="success_url" type="hidden" value="http://oreilly.com/store/
newsletter-thankyou.html" />
<input name="error_url" type="hidden" value="http://oreilly.com/store/
newsletter-signup-error.html" />
<input name="topic_or_dod" type="hidden" value="1" />
<input name="source" type="hidden" value="orm-home-t1-dotd" />
<fieldset>
<input class="email_address long" maxlength="200" name=
"email_addr" size="25" type="text" value=
"Enter your email here" />
<button alt="Join" class="skinny" name="submit" onclick=
"return addClickTracking('orm','ebook','rightrail','dod'
);" value="submit">Join</button>
</fieldset>
</form>
看起来好像很吓人,但是你只要抓住两点:
1.记住对应的名字
2.表格填充的数据真正被提交到哪里去
其他组件的数据提交
同上或者用浏览器模拟然后观察发送的数据上传文件
保持登陆和cookie
import requests
params = {'username': 'Ryan', 'password': 'password'}
r = requests.post("http://pythonscraping.com/pages/cookies/welcome.php", params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------")
print("Going to profile page...")
r = requests.get("http://pythonscraping.com/pages/cookies/profile.php",
cookies=r.cookies)
print(r.text)
或者使用session
import requests
session = requests.Session()
params = {'username': 'username', 'password': 'password'}
s = session.post("http://pythonscraping.com/pages/cookies/welcome.php", params)
print("Cookie is set to:")
print(s.cookies.get_dict())
print("-----------")
print("Going to profile page...")
s = session.get("http://pythonscraping.com/pages/cookies/profile.php")
print(s.text)
HTTP Basic Access Authentication
其他形式的表格问题
看后续章节
