首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python爬虫无法成功发送表单

Python爬虫无法成功发送表单
EN

Stack Overflow用户
提问于 2015-05-25 09:14:36
回答 2查看 76关注 0票数 0

我对Python很陌生,并试图在我的学校网站(基于aspx)上爬行一些信息。

我想做的是:

  1. http://url.of.the.page
  2. 登录
  3. 打开左边的第四个链接

我试图通过使用req = urllib2.Request(url,data) ( data包含id、密码和通过wireshark可以看到的其他信息)以及result = opener.open(req)print result.read()登录我的帐户。

不幸的是,打印出来的结果与原来的登录页面相同,所以我想我没有成功登录,这个结果也是一样的,当我点击第4舔没有登录。(另一个证据是,当我想在网页上获得另一个链接时,我被重定向到登录页面)。

我的问题是:

  1. 我真的没有登录吗?
  2. 如果是这样的话,正确的登录方式是什么?

我的代码如下:

代码语言:javascript
复制
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
from bs4 import BeautifulSoup
import datetime
import time
from urlgrabber.keepalive import HTTPHandler

def get_ViewState(soup):      
    view_input = soup.find(id="__VIEWSTATE")      
    return (view_input['value'])  

def get_EventValidation(soup):  
    event_input = soup.find(id="__EVENTVALIDATION")  
    return event_input['value'] 

cookie = cookielib.CookieJar()
keepalive_handler = HTTPHandler()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),keepalive_handler)
urllib2.install_opener(opener)

__url = 'http://url.of.the.page'

opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
                    ,('Connection', 'Keep-Alive')
                    ,('Referer',__url)] 


page = urllib.urlopen(__url).read()
soup = BeautifulSoup(page)

viewstate = get_ViewState(soup)  
eventvalidation = get_EventValidation(soup)

postdata = urllib.urlencode({
        '__EVENTTARGET':'',
        '__EVENTARGUMENT:':'', 
        '__VIEWSTATE':viewstate, 
        'TxtStudentId':'xxxxxxx',    
        'TxtPassword':'xxxxxxx',
        'BtnLogin':'login',
        '__EVENTVALIDATION':eventvalidation
        })




req = urllib2.Request(
        url = __url,    
        data = postdata  
    )

result = opener.open(req)
print result.read()
# result = opener.open(req)
# print result.info()

# print result    
# print result.read() 

print "------------------------------------------------"

#after login, I need to get the scores table
__queryUrl = 'http://url.of.the.page?key=0'
now = datetime.datetime.now()
opener.addheaders = [('Referer', 'http://url.of.the.page?i='+now.strftime('%H:%M:%S'))]

result = opener.open(__queryUrl)
print result.read()

for item in cookie:    
        print 'Cookie:Name = '+item.name    
        print 'Cookie:Value = '+item.value 
EN

回答 2

Stack Overflow用户

发布于 2015-05-25 14:59:16

登录时,您必须为网站使用编程API,因为它可能会询问您是否是机器人。要单击第四个链接,只需查看网站的源代码(HTML),并找到所需链接的类和ID。然后,通过一些Googling搜索,您可以将其添加到代码中,并且您已经设置好了:)

票数 0
EN

Stack Overflow用户

发布于 2015-05-26 15:34:20

通过捕获包,我发现我的POST消息从服务器获得了一条OK消息,这意味着我成功登录了。

GET消息作为返回得到一个302 found的原因是因为我没有在头中包含一个cookie。我使用的是urllib2,它没有自动将cookie包含在GET消息中。

因此,通过执行以下操作,我将cookie硬编码到标题中:

代码语言:javascript
复制
cookie = cookielib.CookieJar()
ckName = ''
ckValue = ''
for item in cookie:    
        ckName = item.name
        ckValue = item.value

opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
                    ,('Referer', 'http://202.120.108.14/ecustedu/K_StudentQuery/K_StudentQueryLeft.aspx?i='+now.strftime('%H:%M:%S'))
                    ,('Cookie',ckName+'='+ckValue)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30434863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档