python笔记----Python 网络爬虫
1.什么是爬虫
例子:爬取百度首页
import urlib.request #导入网络请求模块
response = urlib.request.urlopen('网址’) #实现网络请求
print(response.read().decode(‘utf-8’)) #打印内容
2.网络爬虫的常用技术
2.1.网络请求
3个常用的模块:
urllib模块
request 该模块定义的打开URL的方法和类
error 该模块主要
parse 该模块用于URL的解析和URL引用
robotparser 该模块用于解析robot.txt文件(爬虫规则文件)
简单例子:
import urllib.request
import urllib.parse #导入解析模块
#创建参数
data = bytes(urllib.parse.urlencode({‘word’:‘hello’}),encoding = ‘utf-8’)
#发送网络请求
response = urllib.request.urlopen(‘http://httpbin.org/post’,data = data)
html = response.read()
print(html)
urllib3模块
官网:http://urllib3.readthedocs.io/en/latest/
简单例子:
import urllib3
#创建PoolManager对象,用于处理与线程的连接与线程安全