所以我試圖用Python來自動執行508合規性檢查。在我們的網站上有幾百頁,現在每個人實際上每週都會瀏覽網站,並嘗試手動輸入所有網址。下面的UIUC鏈接檢查引用者頭部的請求,然後返回該網站的評估。我無法獲得實際工作的請求。我看過所有這些,找不到任何有用的東西。螺旋式的代碼在錯誤信息的下面和下面。使用Python來操縱HTTP頭
def fae(urltofae):
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
#[('Referer': urltofae)]
r = opener.open('http://www.fae.cita.uiuc.edu/evaluate/link/')
print r
fae("http://www.example.com/")
和錯誤:
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in fae
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>
當我試圖嘗試改變Referer標頭(而不是用戶代理),我得到的格式錯誤,而不是它甚至讓請求連雖然格式與它沒有爲用戶代理抱怨的格式相同。
我仍然是一個新的程序員,所以如果我錯過了一些公然的事情,那我非常抱歉,但我已經嘗試了所有我能想到的事情。 提前致謝,歡呼聲。
好吧,我換了我的策略,它的工作。不幸的是,我不知道爲什麼下面的代碼工作,上面的東西一直在錯誤的我,但我已經看到了一些類似的問題(沒有具體的答案)在谷歌周圍,所以我想我應該發佈它。
vlz,感謝幫助,歡呼聲。
def faeRequest2(urltofae):
r = urllib2.Request('http://fae.cita.illinois.edu/evaluate/link/', headers={'User-agent':'Mozilla/5.0', 'Referer':urltofae})
c = urllib2.urlopen(r)
print c.read()