python3的urllib以及urllib2的報錯問題

1.python

urllib.urlencode(params)post

換成學習

urllib.parse.urlencode(params)url

2.code

在python3.3後urllib2已經不能再用,只能用urllib.request來代替
response=urllib2.urlopen('
  File "b.py", line 1, in <module>
ImportError: No module named 'urllib2'

將urllib2給改成urllib.request便可正常運行
import urllib.request
print(urllib.request.__file__)

3.教程

 

在學習爬蟲的時候,用到postdata時,遇到了一個問題:it

POST data should be bytes or an iterable of bytesclass

一開始覺得是本身代碼寫的有問題,對照着教程和本身的代碼,又沒有錯誤。import

在網上查了一下,能夠用下面的一個語句解決。module

data = urllib.parse.urlencode(values).encode(encoding='UTF8')
相關文章
相關標籤/搜索