网站名称设置,seo查询工具,网站做全景图,网站建设所需人力时间前言
在数据分析中#xff0c;爬虫有着很大作用#xff0c;可以自动爬取网页中提取的大量的数据#xff0c;比如从电商网站手机商品信息#xff0c;为市场分析提供数据基础。也可以补充数据集、检测动态变化等一系列作用。可以说在数据分析中有着相当大的作用#xff01;…前言
在数据分析中爬虫有着很大作用可以自动爬取网页中提取的大量的数据比如从电商网站手机商品信息为市场分析提供数据基础。也可以补充数据集、检测动态变化等一系列作用。可以说在数据分析中有着相当大的作用
页面结构介绍
这里主要介绍HTML的一些简单结构需要一点前端的知识可以根据情况直接跳过。
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
bodytable stylewidth:200px;height:200px;border:1px solid red
!-- table 表格可以输入table然后按tab键即可自动补齐tr 表示行结构td 表示列结构--trtd姓名/tdtd年龄/tdtd性别/td/trtrtd张三/tdtd18/tdtd男/td/tr/table
!-- ui li表示无序列无关联关系ol li表示有序列表有关联关系a标签表示超链接这些结构在爬虫的应用场景中非长广--ulli铁锅炖大鹅/lili小鸡炖蘑菇/lili锅包肉/li/ulolli奖励自己/lili睡觉/lili起床/lili读书学习/li/ola hrefhttp://dwqttkx.blog.csdn.net人间无解/a
/body
/html 爬虫相关概念
1、爬虫的概念
爬虫是一种按照一定规则自动抓取网页信息的程序或脚本。通俗解释就是爬虫就像一个小机器人能够自动在互联网上逛网页。它可以把网页上的文字、图片、连接之类的信息抓取下来然后人们可以用这些信息做数据分析等一系列操作。通过程序模拟浏览器去向服务器发送请求获取相应信息
2、爬虫核心 1、爬取网页爬取整个网页包含了网页中的所有内容 2、解析数据将网页中得到的数据进行解析 3、难点爬虫和反爬虫之间的博弈 3、爬虫的用途
一、爬虫可以搜索引擎优化。搜索引擎靠爬虫收集网页信息有了这些信息才能在用户搜索时提供相应的网页结果。
二、爬虫可以做市场调研。企业可以用爬虫获取竞争对手的产品价格、用户评价等信息了解市场动态。
三、爬虫可以用来做数据收集。用于学术研究手机学术文件、统计数据等资料也可以手机社交媒体的数据来分析舆情和用户喜好。
4、爬虫分类 1、通用爬虫 实例百度、360、goole等搜索引擎---伯乐在线 功能访问网页-抓取数据-数据存储-数据处理-提供检索服务 roots协议一个约定俗成的协议添加roots.txt文件来抓取本网站哪些内容不可以被抓取起不到限制作用自己写的爬虫无需遵循。 网站排名(SEO)1、根据pagerank算法值进行排名 2、百度竞价排名(这就是看谁的钱比较多) 缺点1、抓取的数据大多是无用的 2、不能根据用户的需求来竞逐你获取数据 2、聚焦爬虫 功能根据需求实现爬虫程序抓取需要的数据 设计思路1、确定要爬取的url如何获取url 2、模拟浏览器通过HTTP协议访问url获取服务器返回的HTML如何访问 3、解析HTML字符串根据一定规则提取需要的数据如何解析 5、反爬手段 1、User-AgentUser-Agent中文名为用户地阿里简称UA它是一个特殊字符串头使得服务器能够识别客户使用的操作系统及版本、CPU类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
2、代理IP西次代理、快代理
什么是高匿名、匿名和透明代理区别是什么
使用透明代理对方服务器可以知道你使用了代理并且也知道你的真实IP使用匿名代理对方服务器可以知道你使用了代理但不知道你的真实IP使用高匿名代理对方服务器不知道你使用了代理也不知道你的真实ID。
3、验证码访问
4、动态加载网页网站返回的是js数据并不是网页的真实数据selenium驱动真实的浏览器发送请求
5、数据加密分析js代码
urllib库的使用
1、基本使用
首先我们需要导包 import urllib.request 然后需要定义一个url也就是我们要访问网页的地址域名 模拟浏览器想发送请求获取到响应信息。
# 使用urllib来获取百度首页的源码
import urllib.request
# 定义一个url即我们要访问的地址
url http://www.baidu.com
# 模拟浏览器向服务端发送请求
response urllib.request.urlopen(url)
# 获取响应中的页面源码
#read()返回的数据是字节形式的二进制数据这时我们需要decode进行解码操作
content response.read().decode(utf-8)
print(content)
2、urllib的一个类型和六个方法
import urllib.request
url http://www.baidu.com
response urllib.request.urlopen(url)
# 一个类型
print(type(response)) # class http.client.HTTPResponse
# 六个方法
content response.read() # 一个字节一个字节的读
content response.read(5) # 只读取前五个字节
# 一行一行的读取每次只读取一行
content response.readline()
# 读取全部行
content response.readlines()
# 返回状态码200则逻辑没问题如果是404、500是有问题的
print(response.getcode())
# 返回url地址
print(response.geturl())
# 返回状态信息响应头
print(response.getheaders())3、资源下载
基本语法 urllib.request.urlretrieve(url,filename) # url代表的是下载的路径filename代表文件的名字 import urllib.request
# 下载一个网页
url_page http://www.baidu.com
urllib.request.urlretrieve(url_page, baidu.html)
# 下载图片
url_img https://img1.baidu.com/it/u2414145851,4105017234fm253fmtautoapp138fJPEG?w800h1067
urllib.request.urlretrieve(url_img, jujingyi.jpg)
# 下载视频
url_video https://vdept3.bdstatic.com/mda-qkh666t7qtjd02w7/cae_h264/1731906619294350169/mda-qkh666t7qtjd02w7.mp4?v_from_shkapp-haokan-nanjingauth_key1732795382-0-0-0627520a3ef7ab8203af6dfe76c639debcevod_channelsearchbox_feedpd1cr0cd0pt3logid0182283432vid582295739326229165klogid0182283432abtest122021_2
urllib.request.urlretrieve(url_video, shipin.mp4) 4、请求对象的定制
UA介绍User Agent中文名为用户代理 简称UA它是一个特殊字符串头使得服务器能够识别客户使用的操作系统及版本、CPU类型、浏览器及版本。浏览器内核、浏览器渲染引擎、浏览器语言、浏览器插件等。
语法 request urllib.request.Request() import urllib.request
url https://www.baidu.com
response urllib.request.urlopen(url)
content response.read().decode(utf-8)
print(content) # 我们会发现我们得到的内容相当少这是因为https是由ssl协议的
上述错误代码便是我们遇到的第一个反爬手段即User Agent。在这里我们说一下url的组成 协议http/httpshttps是有ssl加密的更加安全 主机即域名——www.baidu.com 端口号比如http的端口号为80https为443MySQL为3306oracle为1521redis为6379mongodb为27017等 路径s 参数后面的数据 锚点 import urllib.request
url https://www.baidu.com
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
# 因为urlopen()方法中不能存储字典,因此headers不能传入,此时需要定制请求对象
request urllib.request.Request(url, headersheaders)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
print(content)
4.1、get请求方式urllib.parse.quote() 周杰伦的网页域名https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wd%E5%91%A8%E6%9D%B0%E4%BC%A6 需求:获取https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wd周杰伦的网页源码 import urllib.request
url https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wd周杰伦
headers {user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url, headersheaders)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
print(content)
# UnicodeEncodeError: ascii codec cant encode characters in position 42-44: ordinal not in range(128)
我们会发现并不能正常运行而是报了上述错误这是因为周杰伦三个字超出ASCII码值检索的范围需要我们把周杰伦三个字变成Unicode编码这个时候需要用到quote方法。
import urllib.request
import urllib.parse
name urllib.parse.quote(周杰伦)
url https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wdname
headers {user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url, headersheaders)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
print(content)
4.2、get请求方式urllib.parse.urlencode()
但quote方法的缺点很显然需要一次次的对中文汉字进行转换每次都需要调用相当麻烦所以我们需要一种新的方法来解决这个问题而这个方法便是urlencode方法。应用场景多参数需要进行Unicode编码的时候。
# 获取https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wd周杰伦sex男location中国台湾网页源码
import urllib.request
import urllib.parse
data {wd:周杰伦,sex:男,location:中国台湾}
new_data urllib.parse.urlencode(data)
url https://www.baidu.com/s?tn25017023_13_dgch1ieutf-8new_data
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url, headersheaders)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
print(content)
4.3、post请求方式
POST请求是HTTP协议中的一种请求方法。
POST主要用于向指定的资源提交要被处理的数据。比如在网页表单提交数据如用户注册、登录信息、发表评论到服务器时就经常会用到POST请求。和GET请求相比POST请求传递的数据不会像GET请求一样直接显示在URL中这让POST请求更适合传递敏感信息如密码因为数据不会在URL中暴露。我们以百度翻译为例
import urllib.request
import urllib.parse
import json
url https://fanyi.baidu.com/sug
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
data {kw:spider}
# post请求的参数一定要进行编码
data urllib.parse.urlencode(data).encode(utf-8) # 因为data需要是字节类型的数据,所以此时因该进行编码操作
request urllib.request.Request(url, data, headers)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
content json.loads(content)
print(content)
最后结果会返回一个json数据。我们可以使用json.loads()方法将json数据转化为python数据。
4.4、ajax的get请求
我们以爬取豆瓣电影第一页数据并下载到本地为例子
import urllib.request
import urllib.parse
url https://movie.douban.com/j/chart/top_list?type5interval_id100%3A90actionstart0limit20
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url,headersheaders)
repose urllib.request.urlopen(request)
content repose.read().decode(utf-8)
# 下载数据到本地
f open(豆瓣.json,w,encodingutf-8)
f.write(content)
f.close()
那么如何批量下载更多数据呢比如下载前十页为例我们提供三个方法
# ajax的get请求豆瓣电影前十页数据并保存到本地,方法一
# import urllib.request
# import urllib.parse
# url https://movie.douban.com/j/chart/top_list?type5interval_id100%3A90actionstart0limit200
# headers {user-agent:
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
# request urllib.request.Request(url, headersheaders)
# reponse urllib.request.urlopen(request)
# content reponse.read().decode(utf-8)
# f open(豆瓣shi.json,w,encodingutf-8)
# f.write(content)
# f.close()# 方法二通过函数形式每一页都调用一次函数以及get方法以追加的形式把数据写入到文件
# import urllib.request
# import urllib.parse
#
#
# def create_request(page):
# base_url https://movie.douban.com/j/chart/top_list?type5interval_id100%3A90action
# headers {user-agent:
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
# data {start: (page - 1) * 20,
# limit: 20}
# data urllib.parse.urlencode(data)
# url base_url data
# request urllib.request.Request(url, headersheaders)
# response urllib.request.urlopen(request)
# content response.read().decode(utf-8)
# f open(douban.json, a, encodingutf-8)
# f.write(content)
# f.close()
#
# if __name__ __main__:
# start_page int(input(请输入起始页码:))
# end_page int(input(请输入结束页码))
# for page in range(start_page, end_page 1):
# # 每一页都有请求对象的定制
# create_request(page)# 方法三
import urllib.request
import urllib.parse
def create_request(page):base_url https://movie.douban.com/j/chart/top_list?type5interval_id100%3A90actionheaders {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}data {start:(page-1)*20,limit:20}data urllib.parse.urlencode(data)url base_url datarequest urllib.request.Request(url,headers headers)return request
def get_content(request):response urllib.request.urlopen(request)content response.read().decode(utf-8)return content
def down_load(page,content):with open(doubanstr(page).json,w,encodingutf-8) as f:f.write(content)if __name__ __main__:start_page int(input(请输入起始页码:))end_page int(input(请输入结束页码))for page in range(start_page, end_page1):# 每一页都有请求对象的定制request create_request(page)content get_content(request)down_load(page,content) 个人建议以第三种方法来开发因为它更加贴合于企业的项目开发。
4.5、ajax的post请求
import urllib.request
import urllib.parse
def create_quest(page):base_url https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?opcnameheaders {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}data {cname: 郑州,pid:None,pageIndex: page,pageSize: 10}new_data urllib.parse.urlencode(data).encode(utf-8)request urllib.request.Request(base_url, new_data, headers)return request
def get_content(request):response urllib.request.urlopen(request)content response.read().decode(utf-8)# import json# content json.loads(content)# content content[Table1]# for i in content:# position i[addressDetail]# print(position)return content
def down_load(content,page):with open(肯德基str(page).json,w,encodingutf-8) as f:f.write(content)if __name__ __main__:start_page int(input(请输入起始页码:))end_page int(input(请输入终止页码:))for page in range(start_page,end_page1):# 请对象的定制request create_quest(page)# 获取网页源码content get_content(request)# 下载down_load(content,page)
总体来说和上一个案例套路基本一致把基础打牢应该可以轻松解决
URLError/HTTPError 简介1、HTTPError类是URLError类的子类 2、导入的包urllib.error.HTTPError urllib.error.URLError 3、http错误http错误是针对浏览器无法连接到服务器而增加出来的错误提示。引导并告诉浏览者该页是哪里出了问题。 4、通过urllib发送请求的时候有可能会发送失败这个时候如果想让你的代码更加稳定可以通过异常捕获来优化代码 import urllib.request
import urllib.error
url https://blog.csdn.net/2301_80109683/article/details/1439772511
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
try:request urllib.request.Request(url,headersheaders)response urllib.request.urlopen(request)content response.read().decode(utf-8)print(content)
except urllib.error.HTTPError as e:print(e)
Cookie登录
Cookie登录是网站用来识别用户身份的一种方式。用户首次登陆成功后网站服务器会生成含用户登录信息的Cookie并发送浏览器保存。再次访问是浏览器自动发送Cookie给服务器服务器借此确认身份使用户免重复输账号密码。其优点是方便快捷、减轻服务器负担缺点是存在安全风险与隐私问题。
# 微薄的cookie登录,使用场景数据采集时需要绕过登录然后进入到某个页面
import urllib.request
url https://weibo.cn/7860977321/info
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36,# cookie中携带着登录信息如果有登陆之后的cookie我们可以携带着cookie进入到任何页面cookie:_T_WMdc6e834b939c02f2cf63eae686703cdb; SCFAqLqfxclF7261sAej5VukiKWtyKwQC6YWhOjr-DxKfiRwoK4a_pkJ1Wynkwvwq8P2VKl0StItmmIaH0dNf_4sok.; SUB_2A25KSEBRDeRhGeFG7VIY9ynPyT2IHXVpJN2ZrDV6PUJbktANLU_3kW1NeTtdpWKPQR6WSGxb8AKZSDUfjZ1abI_S; SUBP0033WrSXqPxfM725Ws9jqgMF55529P9D9W56bhEyW3jnAWOeE1zBXWuj5NHD95QN1hq71KMNe0zpWs4DqcjMi--NiK.Xi-2Ri--ciKnRi-zNS0nceh.NS0eEeBtt; SSOLoginState1733046273; ALF1735638273}
request urllib.request.Request(url, headersheaders)
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
# 将数据保存到本地
f open(weibo.html, w, encodingutf-8)
f.write(content)
f.close()
# 如果不加cookie我们打开HTML文件是一片空白这是因为请求头信息不够因此访问不成功
Handler处理器
Handler处理器用于处理各种不同类型的URL请求。他是构建自定义网络请求处理流程的关键部分通过不同类型的handler可以实现对HTTP、HTTPS、FTP等协议请求的定制化操作包括处理Cookie、代理、认证等功能。
# handler处理器基本使用使用handler访问百度来获取网页源码
import urllib.request
url http://www.baidu.com
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url, headersheaders)
# 获取handler对象
handler urllib.request.HTTPHandler()
# 通过handler获取openr对象
opener urllib.request.build_opener(handler)
# 调用open方法
response opener.open(request)
content response.read().decode(utf-8)
print(content)代理服务器
代理服务器的常用功能? 1、突破自身IP访问限制 2、访问一些单位或团体内部资源 比如某大学FTP前提是该代理地址在该资源的允许范围之内使用教育网内地址免费代理服务器就可以用于对教育网开放的各类FTP下载上传以及各类资料查询等共享服务。 3、提高放访问速度 通常代理服务器都设置一个较大的硬盘缓冲区当有外界的信息通过时同时也将其保存到缓冲区中当其他用户在访问相同信息时则直接有缓冲区中取出信息传给用户以提高访问速度。 4、隐藏真实IP 上网者也可以通过这种方法隐藏自己的IP免受攻击 代码的配置代理 1、创建Request对象 2、创建ProxyHandler对象 3、用Handler对象创建openr对象 4、使用opener.open的函数发送请求 我们可以在快代理等平台购买一个独享代理这样来爬取数据即使本机IP被封我们依然可以使用代理IP来取访问某个网站并爬取数据。
import urllib.request
url http://www.baidu.com/s?tn25017023_13_dgch1ieutf-8wdip
headers {user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36}
request urllib.request.Request(url, headersheaders)
proxies {http:106.227.10.176:16817
}
handler urllib.request.ProxyHandler(proxiesproxies)
opener urllib.request.build_opener(handler)
response opener.open(request)
content response.read().decode(utf-8)
f open(daili.html, w, encodingutf-8)
f.write(content)
f.close()
即使我们使用了代理IP但是如果我们在短时间内高频次的去访问某一个网站也是避免不了被封的风险的这个时候就需要代理池有一堆高密的代理IP。我们可以自己做一个简易粗糙的代理池
proxies_pool [{http:106.227.10.176:168171},{http:106.227.10.176:1681712},{http:106.227.10.176:16817123}
]
# 假设这些都是真实有效的独享IP利用随机的特性来实现简易的代理池
import random
proxies random.choice(proxies_pool)
print(proxies) # 这样就能出现随机的代理 本次爬虫分享就到这里如有错误欢迎指正感谢观看