简单爬虫示例

爬取抽屉,以及自动登陆抽屉点赞

先查看首页拿到cookie,然后登陆要携带首页拿到的 cookie 才可以通过验证

""""""

# ################################### 示例一:爬取数据(携带请起头) ###################################
"""
import requests
from bs4 import BeautifulSoup r1 = requests.get(
url='https://dig.chouti.com/',
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
) soup = BeautifulSoup(r1.text,'html.parser')
content_list = soup.find(name='div',attrs={"id":"content-list"})
item_list = content_list.find_all(name='div',attrs={'class':'item'})
for item in item_list:
a = item.find(name='a',attrs={'class':'show-content color-chag'})
print(a.text.strip()) """
# ################################### 示例二:登陆点赞 ###################################
"""
import requests
# 1. 查看首页
r1 = requests.get(
url='https://dig.chouti.com/',
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
) # 2. 提交用户名和密码
r2 = requests.post(
url='https://dig.chouti.com/login',
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
},
data={
'phone':'8613121758648',
'password':'woshiniba',
'oneMonth':1
},
cookies=r1.cookies.get_dict()
# 套路 正常用户必然会先访问首页然后再登陆
# 如果你直接登陆必然是爬虫,因此设计在第一次访问首页的时候先创建cookie 并且返回了回去
# 并且要求你第二次访问的时候要带着这个 cookie
) # 3. 点赞
r3 = requests.post(
url='https://dig.chouti.com/link/vote?linksId=20435396',
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
},
cookies=r1.cookies.get_dict()
)
print(r3.text)
""" # ############## 方式二 session 方式 ##############
"""
# 用 session 自动封装好 cookie 不用在以后自己携带
import requests session = requests.Session()
i1 = session.get(url="http://dig.chouti.com/help/service")
i2 = session.post(
url="http://dig.chouti.com/login",
data={
'phone': "8615131255089",
'password': "xxooxxoo",
'oneMonth': ""
}
)
i3 = session.post(
url="http://dig.chouti.com/link/vote?linksId=8589523"
)
print(i3.text)
"""

爬取拉勾网

请求头中存在自定义的验证字段,要想办法拿到才可以正确爬取,以及 Referer 的使用

import re
import requests """
密码加密了的时候
找js 通过 python 实现加密方式
直接把加密后的密文拿来用
""" r1 = requests.get(
url='https://passport.lagou.com/login/login.html',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
) """
有两个奇怪的东西,是网站的防御机制
这两个数据必然是对方发给我们的
要不在响应头里面,要不在响应体里面
响应头看不到。那就去响应体里面找。
""" # 因为不是写在标签里面的。只能用正则来拿了
X_Anti_Forge_Token = re.findall("X_Anti_Forge_Token = '(.*?)'", r1.text, re.S)[0]
X_Anti_Forge_Code = re.findall("X_Anti_Forge_Code = '(.*?)'", r1.text, re.S)[0]
# print(X_Anti_Forge_Token, X_Anti_Forge_Code) r2 = requests.post(
url='https://passport.lagou.com/login/login.json',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'X-Anit-Forge-Code':X_Anti_Forge_Code,
'X-Anit-Forge-Token':X_Anti_Forge_Token,
'Referer': 'https://passport.lagou.com/login/login.html', # 上一次请求地址是什么?很多网站会要求带着个才可以继续
},
data={
"isValidate": True,
'username': '',
'password': 'ab18d270d7126ea65915c50288c22c0d', # 直接发密文了
'request_form_verifyCode': '',
'submit': ''
},
cookies=r1.cookies.get_dict()
)
print(r2.text)

自动登陆GitHub

scrf_token 的验证

""""""
# ################################### 示例三:自动登录GitHub ###################################
# 1. GET,访问登录页面
"""
- 去HTML中找隐藏的Input标签获取csrf token
- 获取cookie
""" # 2. POST,用户名和密码
"""
- 发送数据:
- csrf
- 用户名
- 密码
- 携带cookie
""" # 3. GET,访问https://github.com/settings/emails
"""
- 携带 cookie
""" import requests
from bs4 import BeautifulSoup # ########################################################## # 访问登陆页面,获取 authenticity_token
i1 = requests.get(
url='https://github.com/login'
)
soup1 = BeautifulSoup(i1.text, features='lxml')
tag = soup1.find(name='input', attrs={'name': 'authenticity_token'})
authenticity_token = tag.get('value') # authenticity_token 拿到
c1 = i1.cookies.get_dict()
i1.close() # 携带authenticity_token和用户名密码等信息,发送用户验证
form_data = {
"authenticity_token": authenticity_token, # 放在请求体中发过去
"utf8": "",
"commit": "Sign in",
"login": "",
'password': ''
} i2 = requests.post(
url='https://github.com/session',
data=form_data,
cookies=c1
)
c2 = i2.cookies.get_dict()
c1.update(c2) # 将两次的 cookie 整合一起
i3 = requests.get('https://github.com/settings/repositories', cookies=c1) soup3 = BeautifulSoup(i3.text, features='lxml')
list_group = soup3.find(name='div', class_='listgroup') from bs4.element import Tag for child in list_group.children:
if isinstance(child, Tag):
project_tag = child.find(name='a', class_='mr-1')
size_tag = child.find(name='small')
temp = "项目:%s(%s); 项目路径:%s" % (project_tag.get('href'), size_tag.string, project_tag.string, )
print(temp)

总结

请求头:

user-agent
referer
host
cookie

特殊请起头,查看上一次请求获取内容。

'X-Anit-Forge-Code':...
'X-Anit-Forge-Token':...

请求体:

- 原始数据
- 原始数据 + token
- 密文
  - 找算法
  - 使用密文

套路:

- post登录获取cookie,以后携带cookie
- get获取未授权cookie,post登录携带cookie去授权,以后携带cookie

爬虫,基于request,bs4 的简单实例整合的更多相关文章

  1. 基于CSOCKET的Client简单实例(转)

    原文转自 http://blog.csdn.net/badagougou/article/details/78410382 第一步:创建一个基类为CSOCKET类的新类,Cclient,并在主对话框类 ...

  2. 基于H5的WebSocket简单实例

    客户端代码: <html> <head> <script> var socket; if ("WebSocket" in window) { v ...

  3. 【转】Android Https服务器端和客户端简单实例

    转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...

  4. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序

    SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论   异常汇总:http://www ...

  5. 【转】基于Python的接口测试框架实例

    下面小编就为大家带来一篇基于Python的接口测试框架实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   背景 最近公司在做消息推送,那么自然就会产生很多接口,测试 ...

  6. 基于Python的接口测试框架实例

    文章来源:http://www.jb51.net/article/96481.htm 下面小编就为大家带来一篇基于Python的接口测试框架实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考. ...

  7. Python爬虫教程-16-破解js加密实例(有道在线翻译)

    python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...

  8. 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行

    [微信支付]分享一个失败的案例 2018-06-04 08:24 by stoneniqiu, 2744 阅读, 29 评论, 收藏, 编辑 这个项目是去年做的,开始客户还在推广,几个月后发现服务器已 ...

  9. Log4j日志管理的简单实例

    大型项目中非常多情况下要分析程序的日志信息,怎样管理自己的日志信息至关重要. 在应用程序中加入日志记录总的来说基于三个目的 , 监视代码中变量的变化情况,周期性的记录到文件里供其它应用进行统计分析工作 ...

随机推荐

  1. asp.net/wingtip/显示数据和详细信息

    前边我们的工作处于wingtip工程基础建设阶段,先是建立了“数据访问层”,然后设计建设了“UI和导航”的框架,接下来要充实工程的内容,显示“数据和详细信息”. 一. 添加数据控件(Data Cont ...

  2. [伟哥开源项目基金会](https://github.com/AspNetCoreFoundation)

    伟哥开源项目基金会 GitHub_base=> 伟哥开源项目基金会 该项目作者为伟哥,GitHub地址:https://github.com/amh1979: 该项目维护者为鸟窝,GitHub地 ...

  3. 智能POS常见问题整理

    智能POS预警值为小于所设的数量,H5就会变为锁定状态 智能POS查看数据库方法: 商米D1:设置-存储设备和USB-内部存储设备-浏览-winboxcash tablet.db为智能POS数据库 W ...

  4. Typora 快捷键

    今天学习了一下这个工具.很轻便,很好用的. 无序列表:输入-之后输入空格 有序列表:输入数字+“.”之后输入空格 任务列表:-[空格]空格 文字 标题:ctrl+数字 表格:ctrl+t 生成目录:[ ...

  5. [翻译]:MySQL Error: Too many connections

    翻译:MySQL Error: Too many connections   前言: 本文是对Muhammad Irfan的这篇博客MySQL Error: Too many connections的 ...

  6. ASP.NET Zero--Migration控制台应用程序

    Migration控制台应用程序 AspNet Zero包含一个工具Migrator.exe,用于轻松迁移数据库.您可以运行此应用程序来创建/迁移host和租户数据库. 该应用程序从它自己的appse ...

  7. jenkins安装详细教程

    一.jenkins简介 jenkins是一个开源的软件项目,是基于java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 1.持续的软件版本 ...

  8. 记录日常Linux常用软件

    yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake yum -y install wget httpd-tools vi ...

  9. Docker:测试环境的准备-建立一台centos测试机

    一.安装虚拟机并配置网络,下面演示在一台工作机上搭建环境 基础准备: 安装VMware-workstation-full-15.0.0-10134415.exe 安装虚拟机,镜像文件:CentOS-7 ...

  10. css3新特性合集

    转自:https://www.cnblogs.com/xiaoxie2016/p/5964694.html (若原作者对此转载有疑问,联系删除,谢谢!) animation    IE10 anima ...