python
  • python-2.7
  • python-3.x
  • web-scraping
  • web-scripting
  • 2017-06-14 3 views 1 likes 
    1

    ページソースを読み込んだ後にPOSTパラメータを使用する方法があるかどうかを知りたい。例:ID番号POSTの前にページソースを読む

    私の現在のコード投稿する前にキャプチャをお読みください。

    import requests 
    id_number = "1" 
    url = "http://www.submitmyforum.com/page.php" 
    data = dict(id = id_number, name = 'Alex') 
    post = requests.post(url, data=data) 
    

    変更可能であるキャプチャは、私はそのパラメータを読みたいhttp://submitforum.com/page.php(OBVない本当のサイト)へのすべてのリクエストの後にあるのとそれを「データ」変数に提出します。

    +2

    あなたはどのようにこのWebページをロードする前に、Webページからデータを取得するために、不思議に思っている:セレン(http://selenium-python.readthedocs.io/)の代わりに、要求モジュールメソッドを使用して

    ? _私は良いschrodingerの猫のジョークを見逃しているように感じる.._ – Arount

    +1

    あなたは取得要求を使用して、ページソースを解析し、captcha IDを取得/それを扱い、正しいcaptchaデータを使って投稿要求を送信することができます。しかし、使用しているcaptchaシステムによっては、常に動作するとは限りません。私は最終的に同じセッションやそのようなものを維持するために使用できるので、この種の目的のためにWebブラウザエミュレーション(例:セレニウムのPython実装)を使用して終了しました。 – Retsim

    +0

    @Arount私は変更可能な値をつかんでデータ変数に追加できるように、Webページのソースを読み込もうとしています。 –

    答えて

    0

    OPコメントで説明したように、セレニウムも使用できますが、ブラウザエミュレーションのないメソッドも存在する可能性があります。

    import re 
    import selenium 
    from selenium import webdriver 
    
    regexCaptcha = "k=.*&co=" 
    url = "http://submitforum.com/page.php" 
    
    # Get to the URL 
    browser = webdriver.Chrome() 
    browser.get(url) 
    
    # Example for getting page elements (using css seletors) 
    # In this example, I'm getting the google recaptcha ID if present on the current page 
    try: 
        element = browser.find_element_by_css_selector('iframe[src*="https://www.google.com/recaptcha/api2/anchor?k"]') 
        captchaID = re.findall(regexCaptcha, element.get_attribute("src"))[0].replace("k=", "").replace("&co=", "") 
        captchaFound = True 
        print "Captcha found !", captchaID 
    except Exception, ex: 
        print "No captcha found !" 
        captchaFound = False 
    
    # Treat captcha 
    # --> Your treatment code 
    
    # Enter Captcha Response on page 
    captchResponse = browser.find_element_by_id('captcha-response') 
    captchResponse.send_keys(captcha_answer) 
    
    # Validate the form 
    validateButton = browser.find_element_by_id('submitButton') 
    validateButton.click() 
    
    # --> Analysis of returned page if needed 
    
    +1

    まさに私が望んでいたものです。ありがとう。 –

    +0

    うれしい! – Retsim

    関連する問題