簡體   English   中英

無法使用請求登錄網站

[英]Unable to log in to the website with Requests

我正在嘗試登錄此網站: https : //archiwum.polityka.pl/sso/loginform抓取一些文章。

這是我的代碼:

import requests
from bs4 import BeautifulSoup

login_url = 'https://archiwum.polityka.pl/sso/loginform'
base_url = 'http://archiwum.polityka.pl'

payload = {"username" : XXXXX, "password" : XXXXX}
headers = {"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0"}

with requests.Session() as session:

    # Login...
    request = session.get(login_url, headers=headers)
    post = session.post(login_url, data=payload)

    # Now I want to go to the page with a specific article
    article_url = 'https://archiwum.polityka.pl/art/na-kanapie-siedzi-len,393566.html'
    request_article = session.get(article_url, headers=headers)

    # Scrape its content
    soup = BeautifulSoup(request_article.content, 'html.parser')
    content = soup.find('p', {'class' : 'box_text'}).find_next_sibling().text.strip()

    # And print it.
    print(content)

但是我的輸出就像這樣:

... [pełna treść dostępna dla abonentów Polityki Cyfrowej]

這就是我的母語

... [full content available for subscribers of the Polityka Cyfrowa]

我的憑據是正確的,因為我可以從瀏覽器完全訪問內容,但不能完全使用“請求”。

對於有關如何通過請求執行此操作的任何建議,我將不勝感激。 還是我必須為此使用硒?

我可以為您提供登錄程序。 其余的,我想,您可以管理自己。 您的payload並不包含獲取有效響應的所有必要信息。 從下面的腳本中填寫兩個字段usernamepassword並運行它。 我想,您將看到已經登錄該網頁時看到的名字。

import requests
from bs4 import BeautifulSoup

payload = {
    'username': 'username here',
    'password': 'your password here',
    'login_success': 'http://archiwum.polityka.pl',
    'login_error': 'http://archiwum.polityka.pl/sso/loginform?return=http%3A%2F%2Farchiwum.polityka.pl'
}
with requests.Session() as session:
    session.headers={"User-Agent":"Mozilla/5.0"}
    page = session.post('https://www.polityka.pl/sso/login', data=payload)
    soup = BeautifulSoup(page.text,"lxml")
    profilename = soup.select_one("#container p span.border").text
    print(profilename)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM