地址栏发送的请求永远都是GET请求
POST一定有表单数据
request:
对象
requests:
模块
isinstance:
判断解析结果类型
例:isinstance(params,dict)
empty():
判断队列里是否有值
例:if not self.request_queue.empty(): # 判断队列里是否有值
return self.request_queue.get()
upper()
用户输入转为大写
例:if request.method.upper() == “GET”: # 用户输入转为大写
selenium
里发送的所有的字符串操作都是Unicode,给数据是Unicode,返回数据也是Unicode
端口:
MySQL
3306
Redis
6379
MongoDB
27017
Fiddler、Charles
本地代理服务器 127.0.0.1:8888
response 的三种取值方式:
-1. response.content
-2. response.text
-3. response.json()
httpbin.org/headers 查看本机的请求报头
httpbin.org/ip 查看当前使用的IP地址
httpbin.org 表示机构性网站
1. Get请求:
query_str
= urllib.urlencode({})
request
= urllib2.Request(url + query_str, headers)
response
= urllib2.urlopen(request)
response.read()
# 网页原始编码字符串,还可以图片音视频等字节流数据
response
= requests.get(url, params={}, headers)
response.content
# 网页原始编码字符串,还可以图片音视频等字节流数据
response.text
# 网页Unicode编码字符串
2. Post请求(必须抓包获取表单数据):
request
= urllib2.Reuqest(url + query_str, data, headers)
urllib2.urlopen(request)
requests.post(url, params, data, headers)
列表 转 字符串 : s = " ".join(l)
字符串 转 列表 : l = s.split()
print : 是字符串格式化输出
通过xpath和bs4提取的字符串数据,都是Unicode
编码
Xpath 永远返回一个列表: 有数据的列表 或 空列表
beautifulSoup4 返回一个列表
Jsonpath操作的是python的数据类型 , 返回的是一个列表
response.read() 只能取一次,取第二次的时候返回空
response的read()方法只能执行一次,后续执行测没有数据(案例豆瓣)
response 类文件对象
%3A表示 :(冒号)
只要返回空列表表示json数据取完了
只要页面返回404表示页面不存在
在python中if判断 表示假的有0、flase、None、空的容器(空字典、空列表)
静态页面:数据保存在网页的HTML中
动态页面:数据不直接保存在HTML中,而是服务器后台单独传输数据,再渲染到页面中。
动态页面获取数据,必须抓包,找出浏览器和服务器之间传递的数据(json、js、xml)
GET: 可能会有查询字符串,但是一定没有表单数据(查询字符串会显示在url后面)
POST: 可能会有查询字符串,但是一定有表单数据(表单数据保存在请求体里发送)
json.loads() : 将 json字符串 转为对应的 Python数据类型(读取)
json.dumps() : 将Python数据类型 转为对应的 Json字符串(写入)
format 格式化输出,不需要指定类型
print('[INFO]:正在发送请求{}...'.format(url))
timeout=3 三秒内没有响应就跳过
response = urllib2.urlopen(request,timeout=3)
UUID: time模块获取时间戳 ,time.time() ,int(time.time()*1000) 154201088869
判断队列里是否有值: .empty()
例如: if not self.request_queue.empty():
return self.request_queue.get()
全部转为小写:.lower()
全部转为大写: .upper()
if raw_input(“按下回车继续抓取(输入q则退出):”).lower() == ‘q’:
break
例: if request .method.upper() == “GET”:
解决编码问题:
#不按Unicode处理,按当前Python解释器编码处理,并写入文件
# 如果是Python3
,默认解释器编码是 utf-8,可以正常写入
# 如果是Python2
,默认解释器编码是 ascii,则报 UnicodeEncodeError
# 解决方案在代码文件顶部修改解释器编码为 utf-8 即可。
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
def save_data(self):
# 不按Unicode处理,按当前Python解释器编码处理,并写入文件
# 如果是Python3,默认解释器编码是 utf-8,可以正常写入
# 如果是Python2,默认解释器编码是 ascii,则报 UnicodeEncodeError
# 解决方案在代码文件顶部修改解释器编码为 utf-8 即可。
json_str = json.dumps(self.item_list, ensure_ascii=False)
with open("tencent.json", "w", ) as f:
f.write(json_str)
# json.dump(self.item_list, open("tencent.json", "w"),ensure_ascii=False)
dumps和dump区别:
dumps
: 操作字符串
dump
: 操作文件
json_str = json.dumps(self.item_list, ensure_ascii=False)
with open("tencent.json", "w", ) as f:
f.write(json_str)
# json.dump(self.item_list, open("tencent.json", "w"))
python中没有unicode文本
\u6df1\u5733 只是一种像unicode而已,是一个utf-8
# 类似Unicode的普通字符串
s = '\u6df1\u5733'
# 解码为真正的Unicode
s.decode('unicode-escape')
u'\u6df1\u5733'
print(s.decode('unicode-escape'))
lxml、xpath、etree,Jasonpath、beautifulsoup用法及区别
#coding:utf-8
python代码里的文字,都是utf-8
unicode_str = gbk_str.decode(“gbk”)
utf8_str = unicode_str.encode(“utf-8”)
lxml、xpath、etree :
from lxml import etree
html_obj = etree.HTML(html)
html_obj.xpath()
from jsonpath import jsonpath
name_list = jsonpath(python_obj, “$…name”)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, “lxml”)
node_list = soup.find_all()
node_list = soup.select()