旅游网站建设规划报告怎么写,一般网站建设,wordpress 二维码,定制高端网站建设报价爬虫概念#xff1a;
通过编写程序#xff0c;模拟浏览器上网#xff0c;然后让其去互联网上抓取数据的过程
分类#xff1a;
1#xff0c;通用爬虫#xff1a;抓取一整张页面数据
2#xff0c;聚焦爬虫#xff1a;抓取页面中的局部内容
3#xff0c;增量式爬虫
通过编写程序模拟浏览器上网然后让其去互联网上抓取数据的过程
分类
1通用爬虫抓取一整张页面数据
2聚焦爬虫抓取页面中的局部内容
3增量式爬虫只会抓取网站中最新更新出来的数据
反爬协议robots.txt协议
http协议
服务器与客户端进行数据交互的一种形式
User-Agent:请求载体的身份标识
Connection:请求完毕是断开还是保持连接
Content-Type 服务器响应回客户端的数据类型
https协议证书认证加密安全的超文本传输协议
1requests模块
作用模拟浏览器发请求
流程1指定url 2,发起请求 3获取响应数据 4持久化存储
1.1爬取搜狗首页
import requests
if __name__ __main__:urlhttps://www.sogou.com/responserequests.get(urlurl)page_textresponse.text#返回字符串形式的响应数据print(page_text)with open(./sougou.html,w,encodingutf-8) as fp:fp.write(page_text)print(爬取数据结束) 1.2网页采集器
User-Agent:请求载体的身份标识
UA伪装让爬虫对应的请求载体身份标识伪装成某一款浏览器
import requests
if __name__ __main__:#UA伪装将对应的UA封装到一个字典里headers{User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400}urlhttps://www.sogou.com/sie?hdqAQxRG-0000#处理URL参数分装到字典里kwinput(enter a word:)param{query:kw}
#对指定URL发起的请求对应的url是携带参数的请求过程中处理了参数responserequests.get(urlurl,paramsparam,headersheaders)page_textresponse.text#返回字符串形式的响应数据fileNamekw.htmlwith open(fileName,w,encodingutf-8) as fp:fp.write(page_text)print(fileName,保存成功) 1.3破解百度翻译
打开百度翻译官网右键检查 发出的是一个post请求携带参数响应数据是一组json数据
import requests
import json#导入模块
if __name__ __main__:post_urlhttps://fanyi.baidu.com/sug#1,指定urlheaders {User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400}#经行UA伪装#3post请求参数处理与get类似wordinput(enter a word:)data{kw:word}#4,发送请求responserequests.post(urlpost_url,datadata,headersheaders)#5,获取响应数据json()方法返回的是obj(提前确定是json类型dic_objresponse.json()#持久化存储fileNameword.jsonfpopen(fileName,w,encodingutf-8)json.dump(dic_obj,fpfp,ensure_asciiFalse)#中文不可以用ASCII码print(over!!)
效果 1.4豆瓣电影爬取 文件类型为json,地址中有参数获取方式为get
import requests
import json#导入模块
if __name__ __main__:urlhttps://movie.douban.com/j/chart/top_listparam{type:24,interval_id:100:90,action:,start:3,#第一个电影limit:20#数量}headers {User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400}responserequests.get(urlurl,paramsparam,headersheaders)#请求list_dataresponse.json()#json类型fpopen(./douban.json,w,encodingutf-8)#生成文件json.dump(list_data,fpfp,ensure_asciiFalse)print(over!!!) 1.5爬取肯德基餐厅
要求统计各个城市共有多少家肯德基餐厅并打印门店信息
请求方式为post文本类型(content-Type)text 参数 import requestsurl http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?opkeyword
# UA伪装
header {User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
}
word input(请输入地址: )
numbers 1
# 页数
number_pages 0
# 第一次检测页数
state True
while numbers ! 0:number_pages 1data {cname: ,pid: ,keyword: word,pageIndex: number_pages,pageSize: 10,}# 请求发送response requests.post(urlurl, datadata, headersheader)text response.textnumbers - 1# 计算页数,因为只需要一次即可if state:# 将列表text转化为字典dictionary eval(text)# 获取第一段Table的页数rowcount dictionary[Table]# 将这个列表中的字典赋给dictsdicts rowcount[0]# 查询rowcount所指的页数numbers dicts[rowcount]if numbers 0:print(抱歉,您所输入的地址没有肯德基餐厅)else:print(f{word}一共有{numbers}家肯德基餐厅)if numbers % 10 0:numbers numbers // 10#整除else:numbers numbers // 10 # 不加一是因为已经检查过一次了state Falseprint(text)