爬虫随笔

爬虫随笔

爬虫的小知识

1、urllib.request.urlopen(“http://baidu.com”): 打开一个网站
2、pip 安装时要在pip的文件下,不是在python中
3、selenium用于网站的驱动和渲染,比如:from selenium import webdriver
4、driver = webdriver.Chrome() #打开一个chrome浏览器
5、driver = webdriver.Chrome()
driver.get(“http://baidu.com”):打开百度
page_source:打印源代码
6、phantomjs:跟chromedriver类似,不会出现浏览器。执行的是js代码,相当于网络控制台,完全在命令行里面操作
7、LXML:提供了xpath网页解析方式
8、pipy.python.org:python的api
9、beautifulsoup:网页解析库,from bs4 import BeautifulSoup
10、response = request.get(“http://baidu.com”)
print(response.text) 打印响应体
print(response.status_code) 打印状态码,200是正常
print(response.header) 打印响应头
print(response.content) 打印二进制数据

爬虫原理

1、爬虫就是模拟浏览器,发送请求,从网页中爬取数据
2、基本流程:
发送请求–>获取响应内容–>解析内容–>保存数据(结构化的存储)
3、network,服务器个浏览器的交互过程,请求和响应
request :
请求方式:GET:请求的信息全都包含在参数中,可以直接通过url直接访问
POST: 比如登录时,比GET多了一个form date,用于存放参数,只能通过表单进入,不能通过url访问
请求url :统一资源定位符,比如一个网页文档,一张图片,一个视频都可以用一个url唯一来确定
请求头(request headers):重要的配置信息,User-Agent
请求体:比如POST的form date,请求的额外信息
response:
响应状态:是一个状态码,比如,300以上的状态码用于跳转,404用于报错
响应头:response headers ,以键值对的形式存在
响应体:包含响应的源代码或二进制数据

urllib 库

1、是python内置的HTTP请求库,最基本的请求库
2、 包含以下几个模块:request(请求模块),error(异常处理模块),parse(url解析模块),robots.txt(判断网站是否可以爬取)

urlopen:

urllib的请求

//urlopen的函数原型
urllib.request.urlopen(url,date=None,[timeout]*,cafile=None,capath=None,cadefault=False,context=None)
//urlopen第一个参数的使用方法,get类型的请求
imoport urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode("utf-8"))
//urlopen第一个参数的使用方法,POST类型的请求,需要往里面传参数
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)
print(response.read())
//urlopen第三个参数timeout的用法,过了timeout时间,还没有响应,就抛出timeout的异常
import urllib.request
response = urllib.request.urlopen("http://httpbin.org/get",timeout =1)
//read  方法是用来打印响应的内容,但是是bytes类型,需要把它转成字符型 
print(response.read().decode("utf-8"))

urllib的响应

//打印出响应的类型
import urllib.request
response = urllib.request.urlopen("http://www.python.org")
print(type(response))
//状态码 和 响应头
import urllib.request
response = urllib.request.urlopen("http://www.python.org")
print(response.status)
print(response.getheaders())
print(response.getheader("Server"))

Request:用来发送更复杂的请求

//把url构建成了一个request,再传给urlopen,是可行的
import urllib.request
request = urllib.request.Request("http://www.python.org")
response = urllib.request.urlopen(request)
print(response.read().decode("utf-8"))
//定义一个Request类型的Req,代替url,urlopen不能往里面传参数
from urllib import request,parse
url = "http://httpbin.org/post"
headers = {"User_Agent":"Mozilla/4.0(comoatible;MSIE 5.5;Winsows NT)","Host":"httpbin.org"}
dict = {"name":"Germey"}
data = bytes(parse.urlencode(dict),encoding = "utf-8")
req = request.Request(url=url,data = data,headers = headers,method = "POST")
response = request.urlopen(req)
print(response.read().decode("utf-8"))
//用req.add_header()方法替代往req中传header,结果跟上面的一样
from urllib import request,parse
url = "http://httpbin.org/post"
dict = {"name":"Germey"}
data = bytes(parse.urlencode(dict),encoding = "utf-8")
req = request.Request(url=url,data = data,method = "POST")
req.add_header("User_Agent","Mozilla/4.0(comoatible;MSIE 5.5;Winsows NT)")
response = request.urlopen(req)
print(response.read().decode("utf-8"))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值