Python实现模拟登录及表单提交的方法
2019/7/13 21:01:17
本文主要是介绍Python实现模拟登录及表单提交的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了Python实现模拟登录及表单提交的方法。分享给大家供大家参考。具体实现方法如下:
# -*- coding: utf-8 -*- import re import urllib import urllib2 import cookielib #获取CSDN博客标题和正文 url = "http://blog.csdn.net/[username]/archive/2010/07/05/5712850.aspx" sock = urllib.urlopen(url) html = sock.read() sock.close() content = re.findall('(?<=blogstory">).*(?=<p class="right artical)', html, re.S) content = re.findall('<script.*>.*</script>(.*)', content[0], re.S) title = re.findall('(?<=<title>)(.*)-.* - CSDN.*(?=</title>)', html, re.S) #根据上文获取内容新建表单值 blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #百度博客标题 'spBlogText': content[0].decode('utf-8').encode('gbk'),#百度博客内容 'ct': "1", 'cm': "1"} del content del title #模拟登录 cj = cookielib.CookieJar() #用户名和密码 post_data = urllib.urlencode({'username': '[username]', 'password': '[password]', 'pwd': '1'}) #登录路径 path = 'https://passport.baidu.com/?login' opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('User-agent', 'Opera/9.23')] urllib2.install_opener(opener) req = urllib2.Request(path, post_data) conn = urllib2.urlopen(req) #获取百度发布博客的认证令牌 bd = urllib2.urlopen(urllib2.Request('http://hi.baidu.com/[username]/creat/blog')).read() bd = re.findall('(?<=bdstoken\" value=\").*(?=ct)', bd, re.S) blog['bdstoken'] = bd[0][:32] #设置分类名 blog['spBlogCatName'] = 'php' #比较表单发布博客 req2 = urllib2.Request('http://hi.baidu.com/[username]/commit', urllib.urlencode(blog)) #查看表单提交后返回内容 print urllib2.urlopen(req2).read() #请将[username]/[password]替换为您的真实用户名和密码
希望本文所述对大家的Python程序设计有所帮助。
这篇关于Python实现模拟登录及表单提交的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04Python外包有哪些常见的主要库-icode9专业技术文章分享
- 2024-10-02基于深度学习的钢铁缺陷检测系统(yolov8、注意力机制、PyQt5界面、Python)
- 2024-10-01怎么用python搭建一个系统-icode9专业技术文章分享
- 2024-09-30Python基础入门教程
- 2024-09-30获取参数学习:Python中的基础教程
- 2024-09-30Python编程基础详解
- 2024-09-29点击加载学习:Python编程基础教程
- 2024-09-29数据科学五大Python前端库:第二部分
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础