2012-02-22 2 views
0

는 나는이 스크립트를 사용하여 웹 페이지의 HTML을 얻을 수에 기록되고 필요로하는 웹 페이지의 HTML을 구문 분석하려고 해요 :Python : 로그인해야하는 웹 페이지의 HTML을 어떻게 구문 분석합니까? .

from urllib2 import urlopen 
from BeautifulSoup import BeautifulSoup 
import re 

webpage = urlopen ('https://www.example.com') 
soup = BeautifulSoup (webpage) 
print soup 
#This would print the source of example.com 

을하지만 '웹 페이지의 소스를 얻으려고 노력 로그인하는 것이 더 어렵다는 것을 증명합니다. ('https://www.example.com')을 ('https : // user : [email protected]')으로 바꾸려고했지만 잘못된 URL 오류가 발생했습니다.

누구든지이 작업을 수행 할 수 있습니다. 미리 감사드립니다.

+1

다음과 같이 셀레늄 모듈을 사용하여 그것을 할 수 있습니다. HTTP 기본 인증을 사용하는 경우 쿼리에 하나의 HTTP 헤더를 추가하는 것만으로 충분하지만 양식과 captcha가 있으면 전체 게임이 다릅니다. –

+0

기계화를 시도하십시오 : http://wwwsearch.sourceforge.net/mechanize/ 그러나 로그인하는 방법을 알고 있어야합니다. – sherpya

답변

3

셀레늄 WebDriver (http://seleniumhq.org/projects/webdriver/)가 여기에 적합 할 수 있습니다. 페이지에 로그인 한 다음 HTML의 내용을 인쇄 할 수 있습니다. 다음 예는 다음과 같습니다

from selenium import webdriver 

# initiate 
driver = webdriver.Firefox() # initiate a driver, in this case Firefox 
driver.get("http://example.com") # go to the url 

# log in 
username_field = driver.find_element_by_name(...)) # get the username field 
password_field = driver.find_element_by_name(...)) # get the password field 
username_field.send_keys("username") # enter in your username 
password_field.send_keys("password") # enter in your password 
password_field.submit() # submit it 

# print HTML 
html = driver.page_source 
print html 
+0

** 멋진 **입니다. –

+0

이로 인해 @ David542 => _ConnectionResetError 오류가 발생합니다 : [WinError 10054] 기존 연결이 원격 호스트에 의해 강제로 닫힘 _ – Prometheus

1

당신은 나중에 받았다 쿠키를 저장하고 로그인 할 필요가 페이지를 다운로드하는 동안을 제공합니다.

+1

이것은이를 수행하는 한 방법이지만 실제로 웹 사이트에서 인증을 요구하는 방법에 달려 있습니다. –

+1

@ AndréCaron : 그러나 이것은 일반적으로 captcha가 필요한 특수한 경우가 아닌 한 사용자 인터페이스가있는 모든 웹 사이트에 적용 할 수 있습니다.이 경우 많은 옵션이 없으며 사이트 소유자는 아마 그렇지 않습니다 사이트를 긁어서 다른 장애물이있을 수 있기를 바랍니다.) –

2

을 내가 당신을 제안, (로그인 자격 증명) 로그인 폼에 POST 요청을 전송 시도 할 수 있습니다 Mechanize를 사용할 수 있습니다.

Python mechanize login to website

은에서가 설정을 쿠키 등이 알아서 할 수 있도록 브라우저 객체를 기계화.

양식 및 링크를 반복 할 수 있습니다. 예 :

for form in browser.forms(): 
    print form 

원하는 양식을 선택하고 원하는 방식으로 입력 할 수 있습니다.

0

우리는 그것은 당신이 문제의 웹 사이트는 당신이 인증을 요구하는 방법을 알려하지 않는 한 당신을 도와 어려운

from selenium.selenium import selenium 
from selenium import webdriver 
import time 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
import webbrowser 


# initiate 
my_browser = webdriver.Firefox() 
my_browser.get("fill with url of the login page ") 
try: 
    my_browser.implicitly_wait(35) 
    username_field = my_browser.find_element_by_name(' enter the value of the name attribute')#value of the name attribute in the source code 
    password_field = my_browser.find_element_by_name('enter the value of the name attribute') 
    username_field.send_keys("fill_with password") 
    password_field.send_keys("fill with User_name") 
    password_field.submit() # submit it 



finally: 

    print 'Look Into the Browser' 
관련 문제