2016-08-04 18 views
1

我正在學習Python,我試圖使用BeautifulSoup解析使用PHP製作的網頁。我的問題是我的腳本顯示此錯誤:未定義的索引:HTTP_ACCEPT_LANGUAGE使用BeatifulSoup/Python

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"> 
<h4>A PHP Error was encountered</h4> 
<p>Severity: Notice</p> 
<p>Message: Undefined index: HTTP_ACCEPT_LANGUAGE</p> 
<p>Filename: hooks/detecta_idioma.php</p> 
<p>Line Number: 110</p> 
</div> 

,當我嘗試這樣做

html = urllib.urlopen(url).read() 
web = BeautifulSoup(html,'html.parser') 
print web 
etiquetas = web('a') 

我認爲這個錯誤的命令行執行我的劇本,而不是使用Web瀏覽器,但是,在執行這個來自Apache的腳本,我有同樣的錯誤。

任何人都知道如何定義解析url?

回答

0

看起來像頁面要求您將Accept-Language標頭與您的請求一起傳遞。 requests

import requests 

url = "my url" 

response = requests.get(url, headers={"Accept-Language": "en-US,en"}) 
html = response.content 
web = BeautifulSoup(html, 'html.parser')