python爬虫登录豆瓣_Python爬虫——爬豆瓣登录页面

直接上代码

import urllib.request

import http.cookiejar

from lxml import etree

# from spiderImg import getImg

head = {

‘Connection‘: ‘Keep-Alive‘,

‘Accept‘: ‘text/html, application/xhtml+xml, */*‘,

‘Accept-Language‘: ‘en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3‘,

‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko‘

}

def makeMyOpener(head):

cj = http.cookiejar.CookieJar()

opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

header = []

for key,value in head.items():

elem = (key,value)

opener.addheaders = header

return opener

oper = makeMyOpener(head)

uop = oper.open(‘https://accounts.douban.com/login‘, timeout = 1000)

data = uop.read()

html = data.decode()

spath = ‘./doubanLogin.html‘

f=open(spath,"w",encoding=‘utf-8‘)

f.write(html)

f.close()

print(html)

原文:http://www.cnblogs.com/wangyuhangboke/p/7827838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值