Python Post and Get 登陆web后台系统并抓取页面
#coding=utf8
#! /usr/bin/env python import httplib
import re
import socket
import urllib timeout = 60
socket.setdefaulttimeout(timeout) def getTable(): f = open('kvpage.html')
page = f.readlines()
f.close()
pattern = re.compile(r'.*<tbody>(.*?)</tbody>.*') for line in page:
#print line
m = pattern.match(line.strip())
if m is not None:
return m.group(1) return None def extractKvEvents(content): #init result
table = [] #init pattern
patternTR = re.compile(r"<tr>(.*?)</tr>")
patternTD = re.compile(r'<td class="confluenceTd">(.*?)</td>') #search all the rows
allrows = patternTR.findall(content)
if allrows is not None:
for row in allrows:
#print row
cols = patternTD.findall(row)
if cols is not None: table.append(cols) return table def outputToExcel(table):
for row in table:
print row def loginWiki(): httpClient = None
try:
params = urllib.urlencode({'os_username': 'xxxx@xxx.com',
'os_password': 'xxxx',
'login': 'Log In'}) headers = {"Content-type": "application/x-www-form-urlencoded"
, "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"} httpClient = httplib.HTTPConnection("xxx.com", 8080, timeout=30)
httpClient.request("POST", "/login.action", params, headers) response = httpClient.getresponse()
# print response.status
# print response.reason
# print response.read()
# print response.getheaders()
print response.getheader('Set-Cookie')
cookieFile = open('cookie.txt', 'w')
cookieFile.write(response.getheader('Set-Cookie'))
cookieFile.close()
except Exception, e:
print e
finally:
if httpClient:
httpClient.close() def catchPage():
httpClient = None try:
#read cookie
f = open('cookie.txt')
cookie = f.read().strip()
print cookie
f.close() #init headers
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
'Cookie': cookie} #send request
httpClient = httplib.HTTPConnection('xxx.com', 8080, timeout=30)
httpClient.request('GET', '/xxxPath', headers=headers) #response是HTTPResponse对象
response = httpClient.getresponse()
print response.status
print response.reason htmlPage = open('kvpage.html', 'w')
htmlPage.write(response.read())
htmlPage.close()
except Exception, e:
print e
finally:
if httpClient:
httpClient.close() if __name__ == '__main__': loginWiki()
catchPage()
tablecontent = getTable()
table = extractKvEvents(tablecontent)
outputToExcel(table)
Python Post and Get 登陆web后台系统并抓取页面的更多相关文章
- python爬虫成长之路(一):抓取证券之星的股票数据
获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一.鉴于此,我拾起了Python这把利器,开启了网络爬虫之路. 本篇使用的版本为python3.5,意在抓取证券之星上当天所 ...
- 简易数据分析 12 | Web Scraper 翻页——抓取分页器翻页的网页
这是简易数据分析系列的第 12 篇文章. 前面几篇文章我们介绍了 Web Scraper 应对各种翻页的解决方法,比如说修改网页链接加载数据.点击"更多按钮"加载数据和下拉自动加载 ...
- Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>
Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...
- python爬虫成长之路(二):抓取代理IP并多线程验证
上回说到,突破反爬虫限制的方法之一就是多用几个代理IP,但前提是我们得拥有有效的代理IP,下面我们来介绍抓取代理IP并多线程快速验证其有效性的过程. 一.抓取代理IP 提供免费代理IP的网站还挺多的, ...
- 利用cookies+requests包登陆微博,使用xpath抓取目标用户的用户信息、微博以及对应评论
本文目的:介绍如何抓取微博内容,利用requests包+cookies实现登陆微博,lxml包的xpath语法解析网页,抓取目标内容. 所需python包:requests.lxml 皆使用pip安装 ...
- Python爬虫入门教程 31-100 36氪(36kr)数据抓取 scrapy
1. 36氪(36kr)数据----写在前面 今天抓取一个新闻媒体,36kr的文章内容,也是为后面的数据分析做相应的准备的,预计在12月底,爬虫大概写到50篇案例的时刻,将会迎来一个新的内容,系统的数 ...
- Web Scraping(网页抓取)基本原理 - 白话篇
本文主要介绍 Web Scraping 的基本原理,基于Python语言,大白话,面向可爱的小白(^-^). 易混淆的名称: 很多时候,大家会把,在网上获取Data的代码,统称为"爬虫&qu ...
- python微信聊天机器人改进版,定时或触发抓取天气预报、励志语录等,向好友推送
最近想着做一个微信机器人,主要想要实现能够每天定时推送天气预报或励志语录,励志语录要每天有自动更新,定时或当有好友回复时,能够随机推送不同的内容.于是开始了分析思路.博主是采用了多线程群发,因为微信对 ...
- Python爬虫入门教程 30-100 高考派大学数据抓取 scrapy
1. 高考派大学数据----写在前面 终于写到了scrapy爬虫框架了,这个框架可以说是python爬虫框架里面出镜率最高的一个了,我们接下来重点研究一下它的使用规则. 安装过程自己百度一下,就能找到 ...
随机推荐
- Python中的模块(2)
1.内置模块2.扩展的 例如:django3.自定义的 文件import demodef read(): print('my read func')demo.read()print(demo.mone ...
- GC参数
串行收集器 串行收集器(Serial),是一个相对比较老的回收器,但是它的效率在回收器中相对较好,并且比较稳定.他在进行垃圾回收的过程中,使得应用暂时被挂起,然后启用单条线程去做垃圾回收,所以在进行垃 ...
- 【BZOJ 2007】 2007: [Noi2010]海拔 (平面图转对偶图+spfa)
2007: [Noi2010]海拔 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2504 Solved: 1195 Description YT市 ...
- 「COCI2016/2017 Contest #2」Bruza
「COCI2016/2017 Contest #2」Bruza 解题思路 : 首先对于任意时刻 \(i\) ,硬币一定移动到了深度为 \(i\) 的节点,所以第 \(i\) 时刻 Danel 一定染掉 ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- [USACO06FEC]Milk Patterns --- 后缀数组
[USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...
- POJ 2406 Power Strings 简单KMP模板 strcmp
http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...
- SpringBoot 部署 docker 打包镜像
SpringBoot 部署 docker 打包镜像 环境: 1.代码编写工具:IDEA 2.打包:maven 3.docker 4.linux 7.JDK1.8 8.Xshell 9.Xftp 第一步 ...
- DML、DDL、DCL是什么?
一.DML DML(data manipulation language)数据操纵语言: 我们经常会用到的 INSERT.DELETE.UPDATE.SELECT语句. 主要用来对数据库的数据进行一些 ...
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...