博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python2网页采集案例
阅读量:2066 次
发布时间:2019-04-29

本文共 797 字,大约阅读时间需要 2 分钟。

1.简单直接采集

import urllib2
response=urllib2.urlopen('http://www.xxx.com')
#获取状态吗,如果是200表示获取成功
print response.getcode()
#读取内容
cont=response.read()
print len(cont)
2.带参数采集
import urllib2
#创建Request对象
request=urllib2.Request('http://www.xxx.com')
#添加数据
request.add_data('a','1')
#添加http的header
request.add_header('User-Agent','Mozilla/5.0')
#发送请求获取结果
response=urllib2.urlopen(request)
print response.getcode()
3.带cookie采集
import urllib2,cookielib
#创建cookie容器
cj=cookielib.CookieJar()
#创建1个opener
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#给urllib2安装opener
urllib2.install_opener(opener)
#使用带有cookie的urllib2访问网页
response=urllib2.urlopen('http://www.xxx.com')
print cj
print response.getcode()

print response.read()

特殊采集:

有的采集需要cookie的支持
有的采集需要proxy代理
有的采集需要https加密方式
有的采集是需要做Redirect跳转

转载地址:http://lykmf.baihongyu.com/

你可能感兴趣的文章
SpringBoot配置属性之MQ
查看>>
SpringBoot集成mybatis
查看>>
Shell文本处理三剑客之grep
查看>>
linux查看进程启动时间
查看>>
Linux 基础命令
查看>>
35 个 Java 代码性能优化总结
查看>>
Linux Sed 命令
查看>>
StandardContext 错误
查看>>
如何添加网站favicon.ico图标
查看>>
cvs no such repository 问题
查看>>
MySQL中REGEXP正则表达式
查看>>
服务端UDP双向通信学习资料
查看>>
Mina TCP 编码解码相关资料收集
查看>>
Maven 打包 上传 运行
查看>>
Maven插件wagon-maven-plugin自动化部署
查看>>
使用wagon-maven-plugin插件自动部署项目
查看>>
Maven 打包的三种方式 和 Springboot 分离jar包
查看>>
ActiveMQ中Session设置的相关理解
查看>>
Linux Python 2.7.15
查看>>
Nexus配置Linux Yum Repository
查看>>