python - 在需要身份验证的地方使用 BeautifulSoup

标签 python web-scraping beautifulsoup lan intranet

我正在使用 BeautifulSoup4 和 Python 请求为公司项目抓取 LAN 数据。由于该网站有登录界面,因此我无权访问数据。登录界面是一个弹出窗口,不允许我在没有登录的情况下访问页面源或检查页面元素。我得到的错误是这样的 -

访问错误:未经授权 访问此文档需要用户 ID

This is a screen-shot of the pop-up box (涂黑部分为敏感信息)。它根本没有关于 html 标签的信息,因此我无法通过 python 自动登录。

我尝试过 requests_ntlm、selenium、python requests 甚至 ParseHub,但没有成功。我已经陷入这个阶段一个月了!请提供任何帮助,我们将不胜感激。

下面是我的初始代码:

import requests
from requests_ntlm import HttpNtlmAuth
from bs4 import BeautifulSoup
r = requests.get("www.amazon.in")
from urllib.request import Request, urlopen
req = Request('http://www.cmegroup.com/trading/products/#sortField=oi&sortAsc=false&venues=3&page=1&cleared=1&group=1', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
print r.content
r = requests.get("www.amazon.in",auth=HttpNtlmAuth('user_name','passwd'))
print r.content*

s_data = BeautifulSoup(r.content,"lxml")*
print s_data.content

错误: 文件错误:未经授权

访问错误:未经授权

访问此文档需要用户 ID

这是我手动登录网站后,BeautifulSoup 尝试访问数据时遇到的错误。

最佳答案

如果您使用 BeautifulSoup 并在 Python 3.x 上请求,只需使用:

from bs4 import BeautifulSoup
import requests

r = requests.get('URL', auth=('USER_NAME', 'PASSWORD'))
soup = BeautifulSoup(r.content)

关于python - 在需要身份验证的地方使用 BeautifulSoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987241/

相关文章:

python - conda安装sklearn_crfsuite在spyder中不起作用

python - 通过添加元组的第二个和第三个元素对元组列表进行排序

r - 使用 R 抓取您自己的 Stack Overflow 配置文件

Python 未检测到的 chromebrowser 突然无法在 bet365.com 上运行

python - 从网页中抓取数据属性

Python 日志记录 : unicode symbols is unicode-escaped

python - 通过 iloc 分配 pandas df 切片是通过引用吗?

html - 在vba中按顺序获取html标签

python - 为什么我在不使用 strip() 时会收到与 strip() 相关的错误? (Python)

python - 查找网站上有 soup.findall unicode 问题的页面数