2017-05-23 9 views
0

을 읽을 실패 다음BeautifulSoup로 내가 노력하고 페이지

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-type" content="text/html;charset=utf-8" /> 
<title>Travis Property Search</title> 
<style type="text/css"> 
     body { text-align: center; padding: 150px; } 
     h1 { font-size: 50px; } 
     body { font: 20px Helvetica, sans-serif; color: #333; } 
     #article { display: block; text-align: left; width: 650px; margin: 0 auto; } 
     a { color: #dc8100; text-decoration: none; } 
     a:hover { color: #333; text-decoration: none; } 
    </style> 
</head> 
<body> 
<div id="article"> 
<h1>Please try again</h1> 
<div> 
<p>Sorry for the inconvenience but your session has either timed out or the server is busy handling other requests. You may visit us on the the following website for information, otherwise please retry your search again shortly:<br /><br /> 
<a href="http://www.traviscad.org/">Travis Central Appraisal District Website</a> </p> 
<p><b><a href="http://propaccess.traviscad.org/clientdb/?cid=1">Click here to reload the property search to try again</a></b></p> 
</div> 
</div> 
</body> 
</html> 

내가 같은 컴퓨터의 브라우저를 통해 그러나 URL을 액세스 할 수 있어요 :

from urllib2 import urlopen 
from BeautifulSoup import BeautifulSoup 
url = 'http://propaccess.traviscad.org/clientdb/Property.aspx?prop_id=312669' 
soup = BeautifulSoup(urlopen(url).read()) 
print soup 

은 위의 print 문은 다음을 보여줍니다 그래서 서버는 확실히 내 IP를 차단하지 않습니다. 내 코드에 어떤 문제가 있는지 이해하지 못합니까?

답변

2

먼저 쿠키를 얻어야 만 URL을 방문 할 수 있습니다. 이 비록
urllib2CookieJar 함께 할 수있는, 내가 requests 추천 : 표준 lib 디렉토리하지

import requests 
from BeautifulSoup import BeautifulSoup 

url1 = 'http://propaccess.traviscad.org/clientdb/?cid=1' 
url = 'http://propaccess.traviscad.org/clientdb/Property.aspx?prop_id=312669' 
ses = requests.Session() 
ses.get(url1) 
soup = BeautifulSoup(ses.get(url).content) 
print soup.prettify() 

requests, 당신은 insall 그것을해야합니다. 당신은 urllib2을 사용하려면 :

import urllib2 
from cookielib import CookieJar 

url1 = 'http://propaccess.traviscad.org/clientdb/?cid=1' 
url = 'http://propaccess.traviscad.org/clientdb/Property.aspx?prop_id=312669' 
cj = CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
opener.open(url1) 
soup = BeautifulSoup(opener.open(url).read()) 
print soup.prettify() 
+0

'BeautifulSoup로 수입 BeautifulSoup' 그것이 BS4 가져 오기 BeautifulSoup'에서'안에서? –

+1

@MD. Khairul Basar 그래, 그게 내가 보통 수입하는 방법이지만, 어느 쪽이든 작동하지. –

+0

왜 쿠키를 가져와야 했습니까? 다른 예 쿠키를 필요로하지 않았습니다. – Zanam