dong5300 发表于 2018-8-6 12:26:33

python 爬虫urllib基础示例

import urllib.request  
import urllib.parse
  
import http.cookiejar
  

  
url="http://bbs.chinaunix.net/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LvfR9"
  
postdata=urllib.parse.urlencode({
  
"formhash":"11154664",
  
"loginsubmit":"true",
  
"username":"superli",
  
"password":"123456789",
  
"referer":"http://bbs.chinaunix.net/",
  
"return_type":""
  
}).encode('utf-8')
  
req=urllib.request.Request(url,postdata)
  
req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0")
  
cjar=http.cookiejar.CookieJar()
  
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cjar))
  
urllib.request.install_opener(opener)
  
data=opener.open(req).read()
  
fl=open("/home/urllib/test/11.html","wb")
  
fl.write(data)
  
fl.close()
  
url2="http://bbs.chinaunix.net/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LvfR9"
  
data2=urllib.request.urlopen(url2).read()
  
fl=open("/home/urllib/test/12.html","wb")
  
fl.write(data)
  
fl.close()
页: [1]
查看完整版本: python 爬虫urllib基础示例