博文

目前显示的是 五月 1, 2022的博文

python爬虫入门,8分钟就够了,这是我见过最简单的基础教学

图片
一、基础入门 1.1什么是爬虫 爬虫(spider,又网络爬虫),是指向网站/网络发起请求,获取资源后分析并提取有用数据的程序。 从技术层面来说就是 通过程序模拟浏览器请求站点的行为,把站点返回的HTML代码/JSON数据/二进制数据(图片、视频) 爬到本地,进而提取自己需要的数据,存放起来使用。 小编创建了一个两千人的python交流群,有零基础和工作了的朋友交流,还有相关的电子书与视频下载,欢迎正在你学习python的朋友和已经在工作的朋友都可以进来一起交流哦!群 : 877169862 1.2爬虫基本流程 用户获取网络数据的方式: 方式1:浏览器提交请求--->下载网页代码--->解析成页面 方式2:模拟浏览器发送请求(获取网页代码)->提取有用的数据->存放于数据库或文件中 爬虫要做的就是方式2。 入门python爬虫,10分钟就够了,这可能是我见过最简单的基础教学 1发起请求 使用http库向目标站点发起请求,即发送一个Request Request包含:请求头、请求体等 Request模块缺陷:不能执行JS 和CSS 代码 2获取响应内容 如果服务器能正常响应,则会得到一个Response Response包含:html,json,图片,视频等 3解析内容 解析html数据:正则表达式(RE模块)、xpath(主要使用)、beautiful soup、css 解析json数据:json模块 解析二进制数据:以wb的方式写入文件 4保存数据 数据库(MySQL,Mongdb、Redis)或 文件的形式。 1.3http协议 请求与响应 http协议 Request:用户将自己的信息通过浏览器(socket client)发送给服务器(socket server) Response:服务器接收请求,分析用户发来的请求信息,然后返回数据(返回的数据中可能包含其他链接,如:图片,js,css等) ps:浏览器在接收Response后,会解析其内容来显示给用户,而爬虫程序在模拟浏览器发送请求然后接收Response后,是要提取其中的有用数据。 1.3.1 request (1) 请求方式 常见的请求方式:GET / POST (2)请求的URL url全球统一资源定位符,用来定义互联网上一个唯一的资源 例如:一张图片、一个文件、一段视频都可

Building a Membership System in Django Under 5 Minutes

图片
Photo by  Kelly Sikkema  on  Unsplash Get the logic then everything else will be academic. Developers often come across the need to implement a sort of membership system when selling a particular service on their website, we will do just that in a minute. This article solely focuses on building or integrating a Membership/Subscription system in your existing Django application. Handling payments can be achieved using the Paypal or Stripe API but this aspect will be part of another article. First thing first, we’ll start a new app called Membership. ./manage.py startapp membership Next, we’ll go inside models.py and start adding the necessary models we’ll make use of. Most of the implementation will be done at the database level therefore, properly defining relationships between tables will determine the correctness of our membership application. We will need 3 models — a  Membership  model that holds the information regarding the membership type (Free, Premium, or whatever), the price