【发布时间】:2017-01-31 10:43:37
【问题描述】:
我正在尝试使用 BeautifulSoup 抓取网站。该网站需要登录。
https://www.bahn.de/p/view/meinebahn/login.shtml
研究网络我知道获得授权的一种正确方法是使用requests。
我的代码如下:
url = 'https://www.bahn.de/p/view/meinebahn/login.shtml'
header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5)AppleWebKit 537.36 (KHTML, like Gecko) Chrome","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp ,*/*;q=0.8"}
user = "username"
pwrd = "password"
response = requests.post(url,headers = header, auth=(user, pwrd))
page = requests.get('https://fahrkarten.bahn.de/privatkunde/meinebahn/meine_bahn_portal.go?lang=de&country=DEU#stay')
soup = BeautifulSoup(page.text, 'html.parser')
不幸的是,这不起作用,因为soup 是一个 html 文本,其中包含“您已退出我们的系统”。虽然response的结果是<Response [200]>
我对@987654328@有点挣扎,原因有两个:
- 我对 auth 方法的理解是否正确,即首先发布登录详细信息,然后访问登录“背后”的网站)还是这种方式不同?
- 如何确定网站是否需要更特殊的身份验证方法?在 html 代码中是否有要查找的关键字?
任何帮助将不胜感激,因为我真的很想理解它,而且我显然是“新手”从手册中得到正确的结论(例如http://docs.python-requests.org/en/master/user/authentication/)
【问题讨论】:
标签: python beautifulsoup python-requests