2014-04-11 3 views
0

저는 대학에서 연구중인 infoweb.newsbank.com의 데이터베이스에서 기사를 수집하려고합니다. 내가 다시이 응답을 얻을쿠키가 필요한 웹 사이트의 Python 기사 모음

from bs4 import BeautifulSoup 
import requests 
import urllib 
from requests import session 
import http.cookiejar 


mainLink = "http://infoweb.newsbank.com.proxy.lib.uiowa.edu/iw-search/we/InfoWeb?p_product=AWNB&p_theme=aggregated5&p_action=doc&p_docid=14D12E120CD13C18&p_docnum=2&p_queryname=4" 




def articleCrawler(mainUrl): 
    response = urllib.request.urlopen(mainUrl) 
    soup = BeautifulSoup(response) 
    linkList = [] 
    for link in soup.find_all('a'): 
     print(link) 

articleCrawler(mainLink) 

Unfortunatrly : 지금까지 내 코드입니다 내가 http.cookiejar를 사용하려고했습니다

<html> 
<head> 
<title>Cookie Required</title> 
</head> 
<body> 
This is cookie.htm from the doc subdirectory. 
<p> 
<hr> 
<p> 

Licensing agreements for these databases require that access be extended 
only to authorized users. Once you have been validated by this system, 
a "cookie" is sent to your browser as an ongoing indication of your authorization to 
access these databases. It will only need to be set once during login. 
<p> 
As you access databases, they may also use cookies. Your ability to use those databases 
may depend on whether or not you allow those cookies to be set. 
<p> 
To login again, click <a href="login">here</a>. 
</p></p></p></hr></p></body> 
</html> 

<a href="login">here</a> 

을, 그러나 나는 도서관에 익숙하지 않다. 파이썬 3을 사용하고 있습니다. 누구든지 쿠키를 수락하고 기사에 액세스하는 방법을 알고 있습니까? 고맙습니다.

답변

2

Python3에 익숙하지 않지만 Python2에서 쿠키를 허용하는 표준 방법은 OpenerDirector에 처리기 중 하나로 HTTPCookieProcessor을 포함시키는 것입니다. 그래서

, 이런 식으로 뭔가 :

params = urllib.urlencode({'username': 'someuser', 'password': 'somepass'}) 
opener.open(LOGIN_URL, params) 
:

import cookielib, urllib, urllib2 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) 

opener 지금은 (아마도 사용자 이름과 암호) URL을 열고 통합 된 CookieJar로 수신 쿠키를 배치 준비가되어 있습니다

로그인에 성공하면 opener에 서버가 쿠키 폼에 앉아있는 인증 토큰을 갖게됩니다. , 다시이 Python3에 대한 해석 방법을 잘

f = opener.open(mainLink) 

,하지만 난 당신이 적어도 cookielib.CookieJar보다는 http.cookiejar 싶은 생각 : 그럼 당신은 당신이 처음에 원하는 링크에 액세스 할 수 있습니다. 후자는 쿠키 내용을 클라이언트로 받기보다는 서버로 HTTP 쿠키 내용을 만드는 것입니다.

+0

좋아, 나중에 확인하고 나중에 댓글을 달아 보겠습니다. 고맙습니다. –

관련 문제