首页 > 解决方案 > 使用访问代码 (APS) 打开 URL 的 Python 脚本

问题描述

我正在尝试在 python 中编写一个脚本来访问以下页面的内容:

https://authors.aps.org/Submissions/status/

到目前为止,我尝试的是通过以下方式使用 urllib.urlopen:

import urllib

access_code = 'XXXXXXX'
author = 'xxxxxxx'
url = 'https://authors.aps.org/Submissions/status/'
login_data = {
            'accode':access_code,
            'author':author,
            'submit':'Submit'
        }

login_data = urllib.parse.urlencode(login_data).encode('utf-8')
urllib.request.urlopen(url, login_data)

但我总是得到

HTTP Error 404: Not Found

你有什么建议吗?

非常感谢!

标签: pythonloginautomationurllib

解决方案


你可能想要这样的东西:

import urllib.request
import urllib.parse

access_code = 'XXXXXXX'
author = 'xxxxxxx'

login_data = urllib.parse.urlencode({
    'accode': access_code,
    'author': author
})

url = 'https://authors.aps.org/Submissions/status?utf8=%E2%9C%93&commit=Submit&{login_data}'.format(login_data=login_data)

with urllib.request.urlopen(url) as f:
    print(f.read().decode('utf-8'))

推荐阅读