使用请求和 BeautifulSoup 登录以抓取页面

法比克斯

我需要抓取一个需要登录才能访问的页面。

我尝试使用在 cUrl 中转换的保存登录信息登录,使用请求和 BeautifulSoup,但它不起作用。

我需要登录“https://www.seoprofiler.com/account/login”然​​后抓取如下页面:“https://www.seoprofiler.com/lp/links?q=test.com”

这是我的代码:

from bs4 import BeautifulSoup 
import requests



cookies = {
    'csrftoken': 'token123',
    'seoprofilersession': 'session123',
}

headers = {
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'sec-ch-ua': '^\\^',
    'sec-ch-ua-mobile': '?0',
    'Upgrade-Insecure-Requests': '1',
    'Origin': 'https://www.seoprofiler.com',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Sec-Fetch-Dest': 'document',
    'Referer': 'https://www.seoprofiler.com/account/login',
    'Accept-Language': 'en,en-US;q=0.9,it;q=0.8',
}

data = {
    'csrfmiddlewaretoken': 'token123',
    'username': '[email protected]',
    'password': 'pass123!',
    'button': ''
}



response = requests.post('https://www.seoprofiler.com/account/login',
                             headers=headers, cookies=cookies, data=data)


url = 'https://www.seoprofiler.com/lp/links?q=test.com'
response = requests.get(url, headers= headers, cookies=cookies)
soup = BeautifulSoup(response.content, 'html.parser')
soup.encode('utf-8')
print(soup.title)

我不会使用 selenium,因为我必须抓取大量数据,而且使用 selenium 需要很多时间。

如何登录以抓取登录的页面?谢谢!

慢速

你可以使用 requests.Session!

经过一些试验和错误,我能够使用以下脚本登录并获取项目页面:

import requests

session = requests.Session() # Create new session
session.get(
    "https://www.seoprofiler.com/account/login"
)  # set seoprofilersession and csrftoken cookies

session.post(
    "https://www.seoprofiler.com/account/login",
    data={
        "csrfmiddlewaretoken": session.cookies.get_dict()["csrftoken"],
        "username": "your_email",
        "password": "your_password",
    },
)  # login, sets needed cookies

# Now use this session to get all data you need!
resp = session.get(
    "https://www.seoprofiler.com/project/google.com-fa1b9c855721f3d5"
)  # get main page content

print(resp.status_code) # my output: 200

编辑:

刚刚检查了一件事,似乎不需要检索 seoprofilersession 和 csrftoken cookie,您只需使用您的凭据调用登录帖子(无需 csrfmiddlewaretoken,然后使用您的会话)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

循环页面以便使用BeautifulSoup进行抓取

使用BeautifulSoup网站抓取IMDb页面

如何使用BeautifulSoup和Python抓取页面?

使用BeautifulSoup抓取亚马逊

使用请求和BeautifulSoup在页面上找不到元素

如何使用BeautifulSoup从页面抓取

Web使用BeautifulSoup抓取多个页面

如何使用Python和BeautifulSoup抓取多个Google页面

使用BeautifulSoup,请求和正则表达式的Web抓取程序中的语法错误

使用BeautifulSoup抓取网页

抓取“ __hpKey”的网站,然后在python中使用请求和beautifulsoup登录

如何使用BeautifulSoup创建循环以从源URL抓取多个页面?

Python-使用BeautifulSoup在页面内抓取多个类

使用请求和 BeautifulSoup 抓取重定向的站点

使用 BeautifulSoup 和 Python 抓取多个表格页面

使用 BeautifulSoup 抓取网页

如何使用python BeautifulSoup通过分页抓取页面

使用 BeautifulSoup 在 python 中抓取多个页面

使用 BeautifulSoup 抓取 HTML

使用 BeautifulSoup 从抓取的页面中提取文本

使用beautifulsoup抓取动态加载页面

使用 BeautifulSoup 抓取数据

当数据在页面之间拆分时使用beautifulsoup进行抓取

使用 BeautifulSoup 抓取:从 HTML 页面抓取表格中的特定列

使用 BeautifulSoup 抓取特定国家/地区的产品页面

使用 BeautifulSoup 抓取问题

使用 BeautifulSoup 从多个页面下载多线程文件的网页抓取

使用 BeautifulSoup 在 Steam 中抓取多个页面

请求和 Beautifulsoup <tables>