第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求
第三百二十七节,web爬虫讲解2—urllib库爬虫
利用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) #
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': '',
'pass': ''
}).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请求的更多相关文章
- 六 web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求
利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode("utf-8")将字节转化成字符串 ...
- 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用
第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...
- 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解
第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...
- 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理
第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术.设置用户代理 如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执 ...
- 七 web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理
如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执行下去 1.常见状态吗 301:重定向到新的URL,永久性302:重定向到临时URL,非永久性304: ...
- 八 web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用
使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener()初始化IPinstall_opener()将代理IP设置成全 ...
- 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解
封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...
- 第三百五十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—利用开源的scrapy-redis编写分布式爬虫代码
第三百五十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—利用开源的scrapy-redis编写分布式爬虫代码 scrapy-redis是一个可以scrapy结合redis搭建分布式爬虫的开 ...
- 第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中
第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中 前面我们讲到的elasticsearch( ...
随机推荐
- EditText: EditText自动获取焦点并弹出键盘&EditText不自动获取焦点并且不会弹出键盘
1.EditText不自动获取焦点并且不会弹出键盘 找到EditText的父控件,设置其父控件为: Parent.setFocusable(true); Parent.setFocusableInTo ...
- 聊一聊 Spring 中的线程安全性
Spring与线程安全 Spring作为一个IOC/DI容器,帮助我们管理了许许多多的“bean”.但其实,Spring并没有保证这些对象的线程安全,需要由开发者自己编写解决线程安全问题的代码. Sp ...
- 每日英语:America The Vulgar
'What's celebrity sex, Dad?' It was my 7-year-old son, who had been looking over my shoulder at my c ...
- Android 支付宝接口调用
在近期,公司需要开发一个关于在线支付的模块,所以需要用到第三方支付平台 转载请注明出处:http://blog.csdn.net/ht_android/article/details/45307165 ...
- 【Qt】qt库结构及示例
QT库结构 Qt图形库是一个组织严谨的C++类库,其结构如图所示 细说Qt库 Qt类库中包含了上百个类,结构十分复杂,上图展示了Qt_3.2类库的基本结构. Qt类库中的类可以分成两种类型: 一种是直 ...
- vue2.0的contextmenu右键菜单
1.事情对象 <!DOCTYPE html> <html> <head> <title></title> <meta charset= ...
- scp拷贝提示its a directory 错误
scp拷贝提示its a directory 错误 场景 使用scp的格式是 scp my_file user@ip:/home/directory 之前也一直这么用,没什么错误,莫名其妙 原因定位 ...
- 应用SAP PI实现SAP BW数据仓库对于第三方系统数据完美集成以及DELTA加载的分析
注明:本篇的技术性细节参考了SAP SCN上的一篇SAP PI 和BW集成的文章,本篇文章并不打算过多探讨实现的技术细节,因为在SCN上的这篇英文文章已经完全涵盖了技术细节和配置步骤 大家可以通过搜索 ...
- Eclipse Git下载问题:Internal error; consult Eclipse error log
在使用Git下载代码时偶尔会遇到 Internal error; consult Eclipse error log 这个报错. 简述下个人解决思路: Eclipse 错误日志报错为:org.ecl ...
- C#防止内存泄露的方法
一般程序员()都会这样认为:用C#这样的语言编程的一个好处就是无需再考虑内存的分配和释放.你只需创建对象,然后通过一种叫做垃圾收集的机制来处理这 些对象,也就是说:当它们不再被应用程序需要的时候来自动 ...