爬虫概念:
爬虫分类:
风险分析
反爬机制
反反爬策略
robots.txt协议:文本协议,在文本中指定了可爬和不可爬的数据说明.
需求文档
数据量级: >= 10000
设计业务逻辑(数据分析+机器学习)
数据分类:
requests模块
环境的安装:
爬取搜狗首页对应的页面源码数据
import requests
#step_1
url = ‘https://www.sogou.com‘
#step_2:返回值是一个响应对象
response = requests.get(url=url)
#step_3:text返回的是字符串形式的响应数据
page_text = response.text
#step_4
with open(‘./sogou.html‘,‘w‘,encoding=‘utf-8‘) as fp:
    fp.write(page_text)wd = input(‘enter a key:‘)
url = ‘https://www.sogou.com/web‘
#存储的就是动态的请求参数
params = {
    ‘query‘:wd
}
#一定需要将params作用到请求中
#params参数表示的是对请求url参数的封装
response = requests.get(url=url,params=params)
page_text = response.text
fileName = wd+‘.html‘
with open(fileName,‘w‘,encoding=‘utf-8‘) as fp:
    fp.write(page_text)
print(wd,‘下载成功!‘)
enter a key:周杰伦
周杰伦 下载成功!#解决中文乱码
wd = input(‘enter a key:‘)
url = ‘https://www.sogou.com/web‘
#存储的就是动态的请求参数
params = {
    ‘query‘:wd
}
#一定需要将params作用到请求中
#params参数表示的是对请求url参数的封装
response = requests.get(url=url,params=params)
#手动修改响应数据的编码
response.encoding = ‘utf-8‘
page_text = response.text
fileName = wd+‘.html‘
with open(fileName,‘w‘,encoding=‘utf-8‘) as fp:
    fp.write(page_text)
print(wd,‘下载成功!‘)
enter a key:周杰伦
周杰伦 下载成功!#解决中文乱码&UA伪装
wd = input(‘enter a key:‘)
url = ‘https://www.sogou.com/web‘
#存储的就是动态的请求参数
params = {
    ‘query‘:wd
}
#即将发起请求对应的头信息
headers = {
    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36‘
}
#一定需要将params作用到请求中
#params参数表示的是对请求url参数的封装
#headers参数是用来实现UA伪装
response = requests.get(url=url,params=params,headers=headers)
#手动修改响应数据的编码
response.encoding = ‘utf-8‘
page_text = response.text
fileName = wd+‘.html‘
with open(fileName,‘w‘,encoding=‘utf-8‘) as fp:
    fp.write(page_text)
print(wd,‘下载成功!‘)
enter a key:波晓张
波晓张 下载成功!headers = {
    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36‘
}
url = ‘https://movie.douban.com/j/chart/top_list‘
start = input(‘enter a start:‘)
limit = input(‘enter a limit‘)
#处理请求参数
params = {
    ‘type‘: ‘5‘,
    ‘interval_id‘: ‘100:90‘,
    ‘action‘: ‘‘,
    ‘start‘: start,
    ‘limit‘: limit,
}
response = requests.get(url=url,params=params,headers=headers)
#json返回的是序列化好的对象
data_list = response.json()
fp = open(‘douban.txt‘,‘w‘,encoding=‘utf-8‘)
for dic in data_list:
    name = dic[‘title‘]
    score = dic[‘score‘]
    
    fp.write(name+‘:‘+score+‘\n‘)
    print(name,‘爬取成功‘)
 
fp.close()
enter a start:1
enter a limit10
指环王3:王者无敌 爬取成功
七武士 爬取成功
蝙蝠侠:黑暗骑士 爬取成功
搏击俱乐部 爬取成功
指环王1:魔戒再现 爬取成功
指环王2:双塔奇兵 爬取成功
攻壳机动队 爬取成功
将军号 爬取成功
V字仇杀队 爬取成功
黑客帝国 爬取成功
post_url = ‘http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword‘
city = input(‘enter a city name:‘)
data = {
    ‘cname‘: ‘‘,
    ‘pid‘: ‘‘,
    ‘keyword‘: city,
    ‘pageIndex‘: ‘3‘,
    ‘pageSize‘: ‘2‘,
}
#data参数表示的就是get方法中的params
response = requests.post(url=post_url,data=data,headers=headers)
response.json()
enter a city name:北京
{‘Table‘: [{‘rowcount‘: 73}],
 ‘Table1‘: [{‘addressDetail‘: ‘北京经济开发区西环北路18号F1+F2‘,
   ‘cityName‘: ‘北京市‘,
   ‘pro‘: ‘24小时,Wi-Fi,店内参观,礼品卡,生日餐会‘,
   ‘provinceName‘: ‘北京市‘,
   ‘rownum‘: 5,
   ‘storeName‘: ‘亦庄‘},
  {‘addressDetail‘: ‘通顺路石园西区南侧北京顺义西单商场石园分店一层、二层部分‘,
   ‘cityName‘: ‘北京市‘,
   ‘pro‘: ‘24小时,Wi-Fi,店内参观,礼品卡‘,
   ‘provinceName‘: ‘北京市‘,
   ‘rownum‘: 6,
   ‘storeName‘: ‘石园南大街‘}]}抓包工具进行局部搜索
如果判定出页面中有动态加载的数据如何进行数据的定位?
对一个陌生的网站数据进行爬取前一定要判定你爬取的数据是否为动态加载的!!!
爬取企业详情信息:http://125.35.6.84:81/xk/
分析:
#要请求到没一家企业对应的id
url = ‘http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList‘
data = {
    ‘on‘: ‘true‘,
    ‘page‘: ‘1‘,
    ‘pageSize‘: ‘15‘,
    ‘productName‘: ‘‘,
    ‘conditionType‘: ‘1‘,
    ‘applyname‘: ‘‘,
    ‘applysn‘: ‘‘,
}
fp = open(‘./company_detail.txt‘,‘w‘,encoding=‘utf-8‘)
#该json()的返回值中就有每一家企业的id
data_dic = requests.post(url=url,data=data,headers=headers).json()
#解析id
for dic in data_dic[‘list‘]:
    _id = dic[‘ID‘]
#     print(_id)
    #对每一个id对应的企业详情数据进行捕获(发起请求)
    post_url = ‘http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById‘
    post_data = {
        ‘id‘:_id
    }
    #json的返回值是某一家企业的详情信息
    detail_dic = requests.post(url=post_url,data=post_data,headers=headers).json()
    company_title = detail_dic[‘epsName‘]
    address = detail_dic[‘epsProductAddress‘]
    
    fp.write(company_title+‘:‘+address+‘\n‘)
    print(company_title,‘爬取成功!!!‘)
fp.close()
    
江苏正东生物科技有限公司 爬取成功!!!
通化昌源医药科技有限公司 爬取成功!!!
启励(广州)生物科技有限公司 爬取成功!!!
广州真事美化妆品制造有限公司 爬取成功!!!
广州市暨鼎生物科技有限公司 爬取成功!!!
西安尹千容生物科技有限责任公司 爬取成功!!!
施洛丹(福建)工贸有限公司 爬取成功!!!
莱芜瑶草生物科技有限公司 爬取成功!!!
辽宁婵泉生物药业有限公司 爬取成功!!!
辽宁东宁药业有限公司 爬取成功!!!
广州二天堂制药有限公司 爬取成功!!!
赫莲娜(广州)生物科技有限公司 爬取成功!!!
广州腾信生物科技有限公司 爬取成功!!!
广州娜艾施化妆品有限公司 爬取成功!!!
广州汉方医学生物科技有限公司 爬取成功!!!原文:https://www.cnblogs.com/baozai168/p/13363302.html