python-2.7
  • web
  • web-scraping
  • 2016-09-28 60 views 1 likes 
    1

    我很欣賞提前的幫助。我正在嘗試編寫一個python腳本,將IP地址發佈到下面引用的站點,並將結果打印到終端或文件中,然後立即讀取該文件。Python 2.7發佈,並從網站獲得結果

    這裏是我的腳本:

    #!/usr/bin/env python 
    
    import requests 
    
    
    IP = raw_input("Enter IP address here: ") 
    
    Alert_URL = 'http://www.blacklistalert.org' 
    
    
    def submit_form(): 
        """Submit a form""" 
        payload = IP 
    
        # make a get request 
        resp = requests.get(Alert_URL) 
        print "Response to GET request: %s" % resp.content 
    
        # send POST request 
        resp = requests.post(Alert_URL, payload) 
        print "Headers from a POST request response: %s" % resp.headers 
        # print "HTML Response: %s" %resp.read() 
    
    
    if __name__ == '__main__': 
        submit_form() 
    

    該網站有部分輸入IP地址,在網頁上,和檢查的網站,我發現線路輸入如下:

    <form method=POST onsubmit="document.forms[0].submit.disabled='true';"> 
    IP or Domain <input onclick="this.value='';" name=q value=11.11.154.23> 
    

    我會喜歡發佈一個IP地址,我想使用上面的輸入部分以某種方式查看網站。例如使用raw_input發送到'value ='部分,並獲得結果。

    感謝您的幫助。

    回答

    0

    您需要解析PHPSESSID和崗位:

    import requests 
    from bs4 import BeautifulSoup 
    
    
    ip = raw_input("Enter IP address here: ") 
    
    data = {"q": ip} # ip goes here 
    url = "http://www.blacklistalert.org/" 
    
    with requests.Session() as s: 
        # get the page first to parse 
        soup = BeautifulSoup(s.get(url).content) 
        # extract and add the PHPSESSID 
        PHPSESSID = soup.select_one("input[name=PHPSESSID]")["value"] 
        data["PHPSESSID"] = PHPSESSID 
        # finally post 
        res = s.post(url, data=data) 
        print(res) 
        print(res.content) 
    
    +0

    這偉大工程!謝謝。 – Maximillian

    相關問題