0
我想將cookie打印到文本文件printcooki.txt中。並打開網頁https://www.google.co.in。但最後我得到空白的文本文件和網頁無法打開。我的程序有哪些變化? 。請幫助我。通過python顯示cookie
from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib
import io
object = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(object), HTTPHandler())
webreq = Request("https://www.google.co.in/")
f = opener.open(webreq)
html = f.read()
print html[:10]
print "the webpage has following cookies "
for cookie in object:
print>> cookie
createtext = open("C:\Users\****\Desktop\printcooki.txt", "w")
print>> cookies ' #to save cookies into printcooki.txt
opener.open('https://www.google.co.in') #to open a webpage
不相關,但是,使用'urllib2'模塊會很痛苦。你嘗試過'request'模塊嗎? –
@ShadyProgrammer我的程序運行良好,使用urllib2模塊。現在我可以看到我的餅乾了。添加到這個程序中,我試圖將cookies對象打印到文本文件中並打開網頁,但我無法完成。我在代碼中進行了一些更改,請讓我知道在將文本文件打印到文本文件中並在同一程序中打開網頁時出現錯誤。先謝謝你。 – Martin