- 01-爬取搜狗首页
'''
爬虫分类:
通用爬虫:抓取系统重要组成部分,抓取的是一整张页面的数据
聚焦爬虫:建立在通用爬虫的基础之上,抓取的是页面中指定的局部内容
增量式爬虫:检测网站中数据更新的情况
反爬机制:
门户网站,可以通过制定相应的策略或技术手段
反反爬策略:
robots.txt(君子协议)协议:
规定了哪些可以爬取,哪些不可以爬取
http协议:
--常用请求头信息:
UserAgent:当前的请求载体的身份标识
Connection:请求完毕后,是断开连接close还是保持连接keep-alive
--常用响应头信息:
content-type:服务器响应客户端的数据类型
https协议:
安全的超文本传输协议
加密方式:
对称秘钥加密:
非对称秘钥加密:
证书秘钥加密:
'''
'''
requests模块:模拟浏览器发出请求
使用:
1.指定URL
2.发起请求
3.获取响应数据
4.持久化存储响应数据
'''
import requests
#第一个爬虫实例:爬取搜狗首页
if __name__=='__main__':
#1.指定url
url = 'http://www.sogou.com/'
#2.发起请求
response = requests.get(url)#get方法返回爬取的结果
#3.获取响应数据,.txt返回的是字符串形式的响应数据
page_txt = response.text
print(page_txt)
#4.持久化存储
with open('搜狗.html', 'w', encoding='utf-8') as fp:
fp.write(page_txt)
print('爬取成功')
- 02-爬取指定词条
import requests
'''
UA伪装:将对应的user-agent封装到一个字典中
'''
url = 'https://www.sogou.com/web'
#处理url携带的参数:封装到字典里
keyworld = input('enter a keyword:')
headers = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
param = {
'query':keyworld
}
response = requests.get(url,param,headers=headers)
page_txt = response.text
filename = keyworld+'.html'
with open(filename,'w',encoding='utf-8') as file:
file.write(page_txt)
print(filename,'保存成功!!!')
- 03-百度翻译破解
import requests
#1.指定url
post_url = 'https://fanyi.baidu.com/sug'
#2.进行UA伪装
headers = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
#3.post请求参数处理
word = input('enter a word:')
data = {
'kw' :word
}
#4.请求发送
response = requests.post(post_url,data=data)
#5.获取响应数据:json()方法
dict_obj = response.json()
print(dict_obj)
- 04-豆瓣电影排行榜
import requests
import json
#1.指定url
url = 'https://movie.douban.com/j/new_search_subjects'
#2.UA伪装
headers = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
#3.处理get携带参数
data = {
'sort':'U',
'range':'0,10',
'tags':'',
'genres':'喜剧'
}
#4.发起请求
response = requests.get(url,data=data,headers=headers)
print(response)
dict_data = response.json()
print(dict_data)
- 05-肯德基餐厅查询
import requests
#1.指定url
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
#2.UA伪装
headers = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
#3.post参数处理
param = {
'cname': '',
'pid':'' ,
'keyword': '惠州',
'pageIndex': '1',
'pageSize': '10'
}
response = requests.post(url,headers=headers,data=param)
print(response.json())
- 06-化妆品生产许可
import requests
#批量获取不同企业的id
url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
headers={
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
id_list = [] #存储批量获取的企业的id
#参数的封装
for i in range(1,6):
i_str = str(i)
data = {
'on': 'true',
'page':i_str,
'pageSize': '15',
'productName':'' ,
'conditionType': '1',
'applyname': '',
'applysn':'',
}
json_ids = requests.post(url,headers,data).json()
for dic in json_ids['list']:
id_list.append(dic['ID'])
print(id_list)
#获取企业详情数据
post_url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
for id in id_list:
data = {
'id':id,
}
detail_json = requests.post(post_url,data,headers).json()
print(detail_json)