Python實現(xiàn)模擬登錄及表單提交的方法
本文實例講述了Python實現(xiàn)模擬登錄及表單提交的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
# -*- 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) #根據(jù)上文獲取內(nèi)容新建表單值 blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #百度博客標題 'spBlogText': content[0].decode('utf-8').encode('gbk'),#百度博客內(nèi)容 '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) #獲取百度發(fā)布博客的認證令牌 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] #設(shè)置分類名 blog['spBlogCatName'] = 'php' #比較表單發(fā)布博客 req2 = urllib2.Request('http://hi.baidu.com/[username]/commit', urllib.urlencode(blog)) #查看表單提交后返回內(nèi)容 print urllib2.urlopen(req2).read() #請將[username]/[password]替換為您的真實用戶名和密碼
希望本文所述對大家的Python程序設(shè)計有所幫助。
- Python發(fā)送form-data請求及拼接form-data內(nèi)容的方法
- python處理multipart/form-data的請求方法
- Python模擬瀏覽器上傳文件腳本的方法(Multipart/form-data格式)
- 在python中使用requests 模擬瀏覽器發(fā)送請求數(shù)據(jù)的方法
- kafka-python批量發(fā)送數(shù)據(jù)的實例
- python使用 request 發(fā)送表單數(shù)據(jù)操作示例
- python實現(xiàn)的登錄與提交表單數(shù)據(jù)功能示例
- Python實現(xiàn)網(wǎng)站表單提交和模板
- Python使用requests提交HTTP表單的方法
- Python 自動化表單提交實例代碼
- Python的Django框架中forms表單類的使用方法詳解
- python實現(xiàn)發(fā)送form-data數(shù)據(jù)的方法詳解
相關(guān)文章
jupyter notebook 使用過程中python莫名崩潰的原因及解決方式
這篇文章主要介紹了jupyter notebook 使用過程中python莫名崩潰的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04使用Python分析數(shù)據(jù)并進行搜索引擎優(yōu)化的操作步驟
在互聯(lián)網(wǎng)時代,網(wǎng)站數(shù)據(jù)是一種寶貴的資源,可以用來分析用戶行為、市場趨勢、競爭對手策略等,本文將介紹如何使用Python爬取網(wǎng)站數(shù)據(jù),并進行搜索引擎優(yōu)化,,需要的朋友可以參考下2023-08-08計算機二級python學(xué)習教程(2) python語言基本語法元素
這篇文章主要為大家詳細介紹了計算機二級python學(xué)習教程的第2篇,Python語言基本語法元素,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05Keras自動下載的數(shù)據(jù)集/模型存放位置介紹
這篇文章主要介紹了Keras自動下載的數(shù)據(jù)集/模型存放位置介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python 列表排序方法reverse、sort、sorted詳解
本文給大家介紹的是Python中列表排序方法中的reverse、sort、sorted操作方法,以及他們直接的區(qū)別介紹,有需要的小伙伴可以參考下。2016-01-01Python實現(xiàn)爬取亞馬遜數(shù)據(jù)并打印出Excel文件操作示例
這篇文章主要介紹了Python實現(xiàn)爬取亞馬遜數(shù)據(jù)并打印出Excel文件操作,結(jié)合實例形式分析了Python針對亞馬遜圖書數(shù)據(jù)的爬取操作,以及數(shù)據(jù)打印輸出Excel相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05django自帶的server 讓外網(wǎng)主機訪問方法
今天小編就為大家分享一篇django自帶的server 讓外網(wǎng)主機訪問方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05