python 3.5で、urllib.request使っています。
スクレイピングツールを作っているのですが、cookieが上手く動かず困っています。
コード
'''
def init(self):
self.cookie_file = "cookies.txt"
self.cj = http.cookiejar.LWPCookieJar()
if os.path.exists(self.cookie_file):
elf.cj.load(self.cookie_file)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
urllib.request.install_opener(opener)
'''
ツール起動時にcookiesをロードし、
'''
def del(self):
self.cj.save(self.cookie_file)
'''
と終了時にsaveしたいのですが、
Traceback (most recent call last):
File "test.py", line 296, in <module>
hoge.test(url)
File "test.py", line 278, in hoge
display(soup)
NameError: name 'display' is not defined
Exception ignored in: <bound method test.del of <main.testobject at 0x0000029B0E4BA1D0>>
Traceback (most recent call last):
File "test.py", line 95, in del
File "C:\Users\lib\http\cookiejar.py", line 1875, in save
NameError: name 'open' is not defined
と、エラーが発生してしまいます。
また、連続してurlopenを使うのですが、このやり方でセッションは問題ないのでしょうか。
あなたの回答
tips
プレビュー