美文网首页
Python-豆瓣爬虫登录

Python-豆瓣爬虫登录

作者: 法号少林 | 来源:发表于2017-07-23 13:39 被阅读0次

如何使用requests登录豆瓣并且爬取内容
Note:
1.如果登录之后要去其他页面查看相关内容得记录session

   s=requests.session()
  r = s.post(loginUrl, data=formData, headers=headers
  res=s.get("http://movie.douban.com/mine",cookies=r.cookies,headers=headers)

2.r.history可以记录login之后的302 status

Code:

# -*- encoding:utf-8 -*-  
##############################  
__author__ = "KevinZhou"
__date__ = "2017/7/23"
###############################  

import requests
from bs4 import BeautifulSoup
import urllib.request
import re

loginUrl = 'https://accounts.douban.com/login'
formData = {
    "redir": "http://movie.douban.com/mine",
    "form_email": "******",
    "form_password": "******",
    "login": u'登录',
    "source":"index_nav"
}
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

r = requests.post(loginUrl, data=formData, headers=headers)
page = r.text
print (r.url)

'''''获取验证码图片'''
# 利用bs4获取captcha地址
soup = BeautifulSoup(page, "html.parser")
captchaAddr = soup.find('img', id='captcha_image')['src']
# 利用正则表达式获取captcha的ID
# reCaptchaID = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
# captchaID = re.findall(reCaptchaID, page)

# htm=requests.get("https://accounts.douban.com/login")
#print(htm.content.decode('utf-8'))
a=soup.find("img",class_="captcha_image")
print(a.attrs['src'])
pattern = re.compile(r'id=(\w*)(\W)en&size=(\w*)$')
match=pattern.search(a.attrs['src'])
if match:
    captchaID=match.group()[3:][:-7]
    print(captchaID)

# print captchaID
# 保存到本地
urllib.request.urlretrieve(captchaAddr,"captcha.jpg")
captcha = input('please input the captcha:')

formData['captcha-solution'] = captcha
formData['captcha-id'] = captchaID
s=requests.session()
r = s.post(loginUrl, data=formData, headers=headers)

page = r.text
print(r.url)
print(r.history)
# print(r.cookies)
# print(r.content.decode('utf-8'))
# res=s.get("http://movie.douban.com/mine",cookies=r.cookies,headers=headers)
#print(res.content.decode('utf-8'))
if r.url == 'https://movie.douban.com/mine':
    print('Login successfully!!!')
print
'我看过的电影', '-' * 60
# 获取看过的电影
soup = BeautifulSoup(page, "html.parser")
result = soup.findAll('li')
for item in result:
    s=item.find('a', class_="cover")
    if s is not None:
        # print(s)
        print(s.get("href"))
        print(s.img["alt"])
        # for img in  s:
        #     print(img["alt"])
        #     print(img.alt)

else:
    print
"failed!"

相关文章

  • Python-豆瓣爬虫登录

    如何使用requests登录豆瓣并且爬取内容Note:1.如果登录之后要去其他页面查看相关内容得记录session...

  • 豆瓣爬虫实践-python版

    豆瓣登录,无验证码版: 豆瓣TOP250电影爬虫 python,生活因你而精彩!

  • python爬虫系列-使用selenium模拟豆瓣登录

    title: python爬虫系列-使用selenium模拟豆瓣登录date: 2019-05-14 13:42:...

  • python爬虫模拟豆瓣登录

    好几天过去了,终于可以更新第二篇爬虫,这次想用python爬虫模拟登陆豆瓣并爬取主页上精选内容的标题部分,确认登录...

  • 爬虫登录之豆瓣6.0

    在爬虫的学习中,爬取豆瓣数据是一个很基本的例子,其中模拟登录豆瓣也很常见 当前网上的资源都是针对豆瓣以前的版本实现...

  • python-爬虫(豆瓣电影网)

    这是今天卡住的一个脚本,nnd,截止到现在还有个问题。服气!!爬虫运行没问题,有问题的是我没有找到这个data数据...

  • python-新浪爬虫之模拟登录

    好了,现在讲述针对Ajax异步请求内容的爬虫实例,以新浪微博为例。首先,新浪微博与前面讲述的两个网站不同的是,需登...

  • python爬虫豆瓣网的模拟登录

    思路一、想要实现登录豆瓣关键点 分析真实post地址 ----寻找它的formdata,如下图,按浏览器的F12可...

  • python-豆瓣简单爬虫-urllib和re使用

    案例教程按以下分类: 按爬去内容分成的三种类型,HTML代码包含型、JS生成型、AJAX异步加载型 按是否需要登录...

  • Python-爬虫

    1、导入第三方库: 在这个网址:https://pypi.org,搜索需要导入库的名称 弹出如图所示界面,将pip...

网友评论

      本文标题:Python-豆瓣爬虫登录

      本文链接:https://www.haomeiwen.com/subject/naqmkxtx.html