使用Python登录后的爬取

2024-06-16 08:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习用Python爬行。你知道吗

我的目标是下载文件。你知道吗

我现在正在学习登录,很难。你知道吗

http://www.kif.re.kr/kif2/login/login.aspx?menuid=56

例如,我需要登录从这个站点下载文件。你知道吗

我查了各种资料。你知道吗

Login to website using python

但我想要的网站似乎有点不同。你知道吗

我能够抓取大多数网站,不需要登录。你知道吗

但是,我不能爬网需要登录的网站。你知道吗

所以我真的很想学习这一部分。你知道吗

我的目标是登录,然后在html中查看爬行代码。你知道吗

下面是我的代码。这样做对吗?你知道吗

from requests import session

# ex) ID = abcd  / PW = 1234

payload = {
'ctl00$ContentPlaceHolder1$tbxLoginID' : 'abcd',
'ctl00$ContentPlaceHolder1$tbxLoginPW' : '1234'
}

with session() as c:
    c.post('http://www.kif.re.kr/kif2/login/login.aspx', data=payload)
    response = c.get('What should I write here?')
    # response = c.get('http://example.com/protected_page.php')
    print(response.headers)
    print(response.text)

Tags: 文件代码rehttp目标网站responsesession
1条回答
网友
1楼 · 发布于 2024-06-16 08:24:43

您遗漏了一些登录数据表单,下面是有效负载的样子

payload = { 
    '__LASTFOCUS': '',#empty
    '__VIEWSTATE': 'get this value from the login page source',
    '__VIEWSTATEGENERATOR': 'get this value from the login page source',
    '__EVENTTARGET': '',#empty
    '__EVENTARGUMENT': '',#empty
    '__EVENTVALIDATION': 'get this value from the login page source',
    'ctl00$agentPlatform': '1',
    'ctl00$menu_nav1$tbxSearchWord': '',#empty
    'ctl00$ContentPlaceHolder1$radiobutton':    '0',
    'ctl00$ContentPlaceHolder1$tbxLoginID': 'abcd',
    'ctl00$ContentPlaceHolder1$tbxLoginPW': '1234',
    'ctl00$ContentPlaceHolder1$ibtnLogin.x': '36', #i think this is the mouse cursor position
    #when clicked on login, not sure if its necessary
    'ctl00$ContentPlaceHolder1$ibtnLogin.y': '25'
}

response = c.get('What should I write here?')

写下受保护页面的url!如果你能成功地得到它,那么你就登录了。你知道吗

相关问题 更多 >