python - 使用python 3从网页抓取数据,需要先登录

标签 python web-scraping python-requests

我检查了this question但它只有一个答案,而且有点超出我的理解范围(刚开始使用 Python)。我正在使用 Python 3。

我正在尝试从 this page 抓取数据,但如果你有 BP 帐户,该页面就会有很大不同/更有用。在 BeautifulSoup 为我获取数据之前,我需要程序让我登录。

到目前为止我已经

from bs4 import BeautifulSoup
import urllib.request 
import requests

username = 'myUsername'
password = 'myPassword'

from requests import session

payload = {'action': 'Log in',
       'Username: ': username,
       'Password: ': password}

# the next 3 lines are pretty much copied from a different StackOverflow
# question. I don't really understand what they're doing, and obviously these 
# are where the problem is.

with session() as c:
    c.post('https://www.baseballprospectus.com/manageprofile.php', data=payload)
    response = c.get('http://www.baseballprospectus.com/sortable/index.php?cid=1820315')

soup = BeautifulSoup(response.content, "lxml")

for row in soup.find_all('tr')[7:]:
    cells = row.find_all('td')
    name = cells[1].text
    print(name)

该脚本确实有效,它只是在登录之前从网站中提取数据,所以这不是我想要的数据。

最佳答案

从概念上讲,您的代码没有问题。您使用 session 对象发送登录请求,然后使用同一 session 发送所需页面的请求。这意味着登录请求设置的 cookie 应保留用于第二个请求。如果您想了解有关 Session 对象工作原理的更多信息,请参阅相关的 Requests documentation .

由于我没有棒球招股说明书的有效登录信息,我不得不猜测您发送到登录页面的数据有问题。使用 Chrome 开发者工具中的“网络”选项卡进行快速检查,显示登录页面 manageprofile.php 接受四个 POST 参数:

username: myUsername
password: myPassword
action: muffinklezmer
nocache: some long number, e.g. 2417395155

但是,您要发送一组不同的参数,并为“action”参数指定不同的值。请注意,参数名称必须与原始请求完全匹配,否则manageprofile.php将不接受登录。

尝试用此版本替换有效负载字典:

payload = {
       'action': 'muffinklezmer',
       'username': username,
       'password': password}

如果这不起作用,请尝试添加“nocache”参数,例如:

'nocache': '1437955145'

关于python - 使用python 3从网页抓取数据,需要先登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642416/

相关文章:

python - 如何在 If else 语句上运行 Pytest?

python - Scrapy 非常基本的例子

python - 使用 Pyquery、Requests 和 Gadget 选择器提取 Web 元素

使用 PFX 证书的 Python 请求将不起作用! - OpenSSL.SSL.错误: [ ('PEM routines' ] - PFX to PEM conversion necessary?

python - Windows 上使用 python 请求的 SSL 失败

python - 如何为向 Web 发出 API 请求的函数编写单元测试代码?

python - 即使测试了所有案例,MyPy 也会给出错误 "Missing return statement"

python - 删除python中情感分析中的标点符号

python - Django:快速处理添加不可空字段

java - 如何使用scraper扩展文本