利用python系统自带的urllib库写简单爬虫

urlopen()获取一个URL的html源码
read()读出html源码内容
decode("utf-8")将字节转化成字符串

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html').read().decode("utf-8")
print(html)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="X1pZZnpKWnQAIGkLFisPFT4jLlJNIWMHHWM6HBBnbiwPbz4/LH1pWQ==">

正则获取页面指定内容

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import re
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html').read().decode("utf-8") #获取html源码
pat = "51CTO学院Python实战群\((\d*?)\)" #正则规则,获取到QQ号
rst = re.compile(pat).findall(html)
print(rst) #['325935753']

urlretrieve()将网络文件下载保存到本地,参数1网络文件URL,参数2保存路径

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from urllib import request
import re
import os file_path = os.path.join(os.getcwd() + '/222.html') #拼接文件保存路径
# print(file_path)
request.urlretrieve('http://edu.51cto.com/course/8360.html', file_path) #下载这个文件保存到指定路径

urlcleanup()清除爬虫产生的内存

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from urllib import request
import re
import os file_path = os.path.join(os.getcwd() + '/222.html') #拼接文件保存路径
# print(file_path)
request.urlretrieve('http://edu.51cto.com/course/8360.html', file_path) #下载这个文件保存到指定路径
request.urlcleanup()

info()查看抓取页面的简介

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import re
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html') #获取html源码
a = html.info()
print(a) # C:\Users\admin\AppData\Local\Programs\Python\Python35\python.exe H:/py/15/chshi.py
# Date: Tue, 25 Jul 2017 16:08:17 GMT
# Content-Type: text/html; charset=UTF-8
# Transfer-Encoding: chunked
# Connection: close
# Set-Cookie: aliyungf_tc=AQAAALB8CzAikwwA9aReq63oa31pNIez; Path=/; HttpOnly
# Server: Tengine
# Vary: Accept-Encoding
# Vary: Accept-Encoding
# Vary: Accept-Encoding

getcode()获取状态码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import re
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html') #获取html源码
a = html.getcode() #获取状态码
print(a) #200

geturl()获取当前抓取页面的URL

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import re
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html') #获取html源码
a = html.geturl() #获取当前抓取页面的URL
print(a) #http://edu.51cto.com/course/8360.html

timeout抓取超时设置,单位为秒

是指抓取一个页面时对方服务器响应太慢,或者很久没响应,设置一个超时时间,超过超时时间就不抓取了

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import re
html = urllib.request.urlopen('http://edu.51cto.com/course/8360.html',timeout=30) #获取html源码
a = html.geturl() #获取当前抓取页面的URL
print(a) #http://edu.51cto.com/course/8360.html

自动模拟http请求

http请求一般常用的就是get请求和post请求

get请求

比如360搜索,就是通过get请求并且将用户的搜索关键词传入到服务器获取数据的

所以我们可以模拟百度http请求,构造关键词自动请求

quote()将关键词转码成浏览器认识的字符,默认网站不能是中文

#!/usr/bin/env python
# -*- coding: utf-8 -*- import urllib.request
import re
gjc = "手机" #设置关键词
gjc = urllib.request.quote(gjc) #将关键词转码成浏览器认识的字符,默认网站不能是中文
url = "https://www.so.com/s?q="+gjc #构造url地址
# print(url)
html = urllib.request.urlopen(url).read().decode("utf-8") #获取html源码
pat = "(\w*<em>\w*</em>\w*)" #正则获取相关标题
rst = re.compile(pat).findall(html)
# print(rst)
for i in rst:
print(i) #循环出获取的标题 # 官网 < em > 手机 < / em >
# 官网 < em > 手机 < / em >
# 官网 < em > 手机 < / em > 这么低的价格
# 大牌 < em > 手机 < / em > 低价抢
# < em > 手机 < / em >
# 淘宝网推荐 < em > 手机 < / em >
# < em > 手机 < / em >
# < em > 手机 < / em >
# < em > 手机 < / em >
# < em > 手机 < / em >
# 苏宁易购买 < em > 手机 < / em >
# 买 < em > 手机 < / em >
# 买 < em > 手机 < / em >

 post请求

urlencode()封装post请求提交的表单数据,参数是字典形式的键值对表单数据
Request()提交post请求,参数1是url地址,参数2是封装的表单数据

#!/usr/bin/env python
# -*- coding: utf-8 -*- import urllib.request
import urllib.parse posturl = "http://www.iqianyue.com/mypost/"
shuju = urllib.parse.urlencode({ #urlencode()封装post请求提交的表单数据,参数是字典形式的键值对表单数据
'name': '123',
'pass': '456'
}).encode('utf-8')
req = urllib.request.Request(posturl,shuju) #Request()提交post请求,参数1是url地址,参数2是封装的表单数据
html = urllib.request.urlopen(req).read().decode("utf-8") #获取post请求返回的页面
print(html)

六 web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求的更多相关文章

  1. 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求

    第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...

  2. 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...

  3. 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...

  4. 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理

    第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术.设置用户代理 如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执 ...

  5. 七 web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理

    如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执行下去 1.常见状态吗 301:重定向到新的URL,永久性302:重定向到临时URL,非永久性304: ...

  6. 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...

  7. 八 web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener()初始化IPinstall_opener()将代理IP设置成全 ...

  8. 第三百三十六节,web爬虫讲解2—urllib库中使用xpath表达式—BeautifulSoup基础

    第三百三十六节,web爬虫讲解2—urllib库中使用xpath表达式—BeautifulSoup基础 在urllib中,我们一样可以使用xpath表达式进行信息提取,此时,你需要首先安装lxml模块 ...

  9. 十五 web爬虫讲解2—urllib库中使用xpath表达式—BeautifulSoup基础

    在urllib中,我们一样可以使用xpath表达式进行信息提取,此时,你需要首先安装lxml模块,然后将网页数据通过lxml下的etree转化为treedata的形式 urllib库中使用xpath表 ...

随机推荐

  1. MongoDB资料汇总(转)

    原文:MongoDB资料汇总 上一篇Redis资料汇总专题很受大家欢迎,这里将MongoDB的系列资料也进行了简单整理.希望能对大家有用. 最后更新时间:2013-04-22 1.MongoDB是什么 ...

  2. HTTP Keep-Alive是什么?如何工作?(转)

    add by zhj: 本篇只是Keep-Alive的第一篇,其它文章参见下面的列表. 原文: HTTP Keep-Alive是什么?如何工作? 1. HTTP Keep-Alive是什么?如何工作? ...

  3. springboot整合fastjson 将null转成空字符串

    /** * @Auther: mxf * @Date: 2019/4/18 09:12 * @Description: */ @Configuration @EnableWebMvc public c ...

  4. JS获取当年当月最后一天日期

    <html xmlns="http://www.w3.org/1999/xhtml" > <meta charset="UTF-8"> ...

  5. python学习之路-第四天-模块

    模块 sys模块 sys.argv:参数列表,'using_sys.py'是sys.argv[0].'we'是sys.argv[1].'are'是sys.argv[2]以及'arguments'是sy ...

  6. ES6的十个新特性

    这里只讲 ES6比较突出的特性,因为只能挑出十个,所以其他特性请参考官方文档: /** * Created by zhangsong on 16/5/20. *///    ***********Nu ...

  7. selenium的下拉选择框

    今天总结下selenium的下拉选择框.我们通常会遇到两种下拉框,一种使用的是html的标签select,另一种是使用input标签做的假下拉框. 后者我们通常的处理方式与其他的元素类似,点击或使用J ...

  8. TCP/IP 网络结构

  9. asp.net MVC 强类型视图表单Ajax提交的注意事项

    xmfdsh这几天遇到这么一个问题,在MVC中通过model模型生成的强类型视图的表单,在提交后的回调函数并没有发挥作用.如下图: 如上图,无论是通过Ajax.BeginForm或者Html.Begi ...

  10. 【转】PCA与Whitening

    PCA: PCA的具有2个功能,一是维数约简(可以加快算法的训练速度,减小内存消耗等),一是数据的可视化. PCA并不是线性回归,因为线性回归是保证得到的函数是y值方面误差最小,而PCA是保证得到的函 ...