打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python 模拟登录及表单提交 - 代码分享 - 开源中国社区

01 # -*- coding: utf-8 -*-  
02 import re  
03 import urllib  
04 import urllib2  
05 import cookielib  
06     
07 #获取CSDN博客标题和正文  
08 url = "http://blog.csdn.net/[username]/archive/2010/07/05/5712850.aspx"  
09 sock = urllib.urlopen(url)  
10 html = sock.read()  
11 sock.close()  
12 content = re.findall('(?<=blogstory">).*(?=<p class="right artical)', html, re.S)  
13 content = re.findall('<script.*>.*</script>(.*)', content[0], re.S)  
14 title = re.findall('(?<=<title>)(.*)-.* - CSDN.*(?=</title>)', html, re.S)  
15 #根据上文获取内容新建表单值  
16 blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #百度博客标题  
17         'spBlogText': content[0].decode('utf-8').encode('gbk'),#百度博客内容  
18         'ct': "1",  
19         'cm': "1"}  
20 del content  
21 del title  
22     
23 #模拟登录  
24 cj = cookielib.CookieJar()  
25 #用户名和密码  
26 post_data = urllib.urlencode({'username': '[username]', 'password': '[password]', 'pwd': '1'})  
27 #登录路径  
28 path = 'https://passport.baidu.com/?login'  
29 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
30 opener.addheaders = [('User-agent', 'Opera/9.23')]  
31 urllib2.install_opener(opener)  
32 req = urllib2.Request(path, post_data)  
33 conn = urllib2.urlopen(req)  
34     
35 #获取百度发布博客的认证令牌  
36 bd = urllib2.urlopen(urllib2.Request('http://hi.baidu.com/[username]/creat/blog')).read()  
37 bd = re.findall('(?<=bdstoken\" value=\").*(?=ct)', bd, re.S)  
38 blog['bdstoken'] = bd[0][:32]  
39 #设置分类名  
40 blog['spBlogCatName'] = 'php'  
41 #比较表单发布博客  
42 req2 = urllib2.Request('http://hi.baidu.com/[username]/commit', urllib.urlencode(blog))  
43     
44 #查看表单提交后返回内容  
45 print urllib2.urlopen(req2).read()  
46     
47 #请将[username]/[password]替换为您的真实用户名和密码
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python3之urllib库的使用总结
Python爬虫日记一:爬取豆瓣电影中速度与激情8演员图片
python-31: 极视界大图爬取
[Python从零到壹] 四.网络爬虫之入门基础及正则表达式抓取博客案例
模拟百度登录
爬虫 | urllib入门 糗事百科实战
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服