python(혹은 다른 언어)으로 html 페이지 긁어오기

eminency의 이미지

python의 httplib나... perl의 LWP 같은 것을 이용해서 가능한 것은 알고 있는데요...
일반 html 페이지라면 긁어오겠는데 문제는 로그인이 필요하다는 점입니다. -_-

상황을 간략하게 말씀드리면...

http://baseball.fantasysports.yahoo.com/b1

위 url은 야후의 메이저리그 판타지 게임 사이트입니다. 제가 지금 즐기고 있는 리그의 데이터를 가져다가 개인적으로 좀 분석을 하고 싶은데요(게임에 이기기 위해서 -_-).

보시면 로그인 할 때 https 페이지를 잠깐 거쳐서 다시 html 페이지로 들어가게 됩니다(야후 코리아 아이디 있으신 분도 로그인 가능합니다). 그 후에 제가 있는 리그와 팀 등을 선택해서 원하는 페이지로 들어가게 되는데 문제는 프로그래밍시에 로그인을 어떻게 통과해야 되는 지를 잘 모르겠습니다 ㅡ.ㅡ;

python은 다룰 줄 알지만 httplib 자체에 대해서는 잘 모르기 때문이기도 하지만요.
조언을 부탁드립니다.

dalmagi의 이미지

로그인 폼을 찾아서 get 으로 문자열을 넣어주고 세션ID를 받아오면 되지 않나요? https 는 좀 다른가요? 흠

화이팅(fighting) 말고 화이트닝(whitening) 하면 안되나요.

puaxx의 이미지

이런거 하실려면 http프로토콜에 대해서 정확한 이해가 없으면 만드시지 못하거나 어렵습니다.

ㅎ...그리고 저 같은 경우에는 Perl로 그런 비슷한 작업들을 해주는 것을 자주 만들었구요..저는 오히려 LWP같은거 안쓰고 그냥 직접 소켓으로 모든걸 해결했습니다...https같은 경우에는 ssleay 모듈을 썼구요..생각보단 쉽습니다.

eminency의 이미지

넵... http에 대한 정확한 이해가 없는게 지금 문제입니다 -_-;

문제 안되시면 참고할만한 소스를 얻을 수 있을지 여쭙고 싶네요. 저야 어차피 개인적인 목적이라서...ㅡ.ㅡ

언어는 상관없습니다 ^^;

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

prolinko의 이미지

http 쎄션하고는 별 상관 없는 이야기입니다만 python으로 html 뜯어내는 데는 Beautiful Soup가 좋다고 들었습니다.

http://www.crummy.com/software/BeautifulSoup/documentation.html

http://openlook.org/blog/953

zelon의 이미지

저도 파이썬은 초보입니다만, 어차피 http 의 세션에 뭔가가 있을거라는 생각에 접근해보니 헤더부분에 세션값이 있더군요. 그래서 아래와 같은 형식으로 쿠키를 얻고, 다시 그 값을 넘겨주니 로그인을 유지하더군요.

#login
request = HTTP("dcinside.com")
request.putrequest("GET", "/bbs/login_check.php?user_id=userid&password=passwd&id=board")
request.putheader("Accept", "text/html")
request.putheader("UserAgent", "PythonAgent")
request.putheader("Host", "dcinside.com")
request.endheaders()
 
errorCode, errorMessage, headers = request.getreply()
 
#print errorCode, errorMessage, headers
 
m = re.search(r"Set-Cookie: (.*?);", str(headers))
cookie = m.group(1)
 
#print "k : " + cookie
#print text
 
subject = []
 
request = HTTP("dcinside.com")
request.putrequest("GET", "/bbs/zboard.php?id=board&page=1&select_arrange=headnum&desc=asc&sn=off&ss=on&sc=on&sn1=&divpage=1")
request.putheader("Accept", "text/html")
request.putheader("UserAgent", "PythonAgent")
request.putheader("Host", "dcinside.com")
request.putheader("Cookie", cookie)
request.endheaders()
 
errorCode, errorMessage, headers = request.getreply()
 
#print headers
 
text = request.getfile().read()
print text

위의 소스는 제대로 동작하지 않는겁니다. 제가 다른곳에 쓰려고 만든 부분을 발췌해서 고친 거라서요.

대충 설명드리자면, 파이썬의 HTTP 라이브러리를 이용해서 쿼리를 한 후, 결과로 받은 부분에서 헤더 부분을 얻고, 그 안에서 "Set-Cookie" 부분의 값을 얻습니다.

그리고 다른 페이지를 요청할 때 putheader() 를 이용하여 아까 받은 쿠키 값을 넘겨줍니다.

좋은 하루 되세요 ^^/
http://www.wimy.com

-----------------------------------------------------------------------
GPL 오픈소스 윈도우용 이미지 뷰어 ZViewer - http://zviewer.wimy.com
블로그 : http://blog.wimy.com

thyoo의 이미지

PAMIE를 권하고 싶군요.

http://pamie.sourceforge.net
___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

lacovnk의 이미지

ClientCookie로 전에 로그인 잘 했던 기억이 있습니다. 브라우징하는 것과 비슷하더군요.. python 할 줄 모르는 저도 따라하기로 성공 -o-;

다음 링크를 참고하세요~

http://www.python.or.kr/pykug/ClientCookie

여기에 있는 ClientForm라는 것이 편했습니다 :)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.