标签:列表 加载 数值 好的 查找 types window 初识 shift
通过编写代码,模拟浏览器发送请求,让其去网络上抓去数据的过程。
通用爬虫
聚焦爬虫
通用爬虫和聚焦爬虫的关联:
增量式
1) Chrome
Win7:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1
win10 64
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
2) Firefox
Win7:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0
3) Safari
Win7:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50
4) Opera
Win7:
Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50
5) IE
Win7+ie9:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)
Win7+ie8:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)
WinXP+ie8:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)
WinXP+ie7:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
WinXP+ie6:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
6) 傲游
傲游3.1.7在Win7+ie9,高速模式:
Mozilla/5.0 (Windows; U; Windows NT 6.1; ) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12
傲游3.1.7在Win7+ie9,IE内核兼容模式:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
7) 搜狗
搜狗3.0在Win7+ie9,IE内核兼容模式:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)
搜狗3.0在Win7+ie9,高速模式:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0
8) 360
360浏览器3.0在Win7+ie9:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
9) QQ浏览器
QQ浏览器6.9(11079)在Win7+ie9,极速模式:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201
QQ浏览器6.9(11079)在Win7+ie9,IE内核兼容模式:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E) QQBrowser/6.9.11079.201
10) 阿云浏览器
阿云浏览器1.3.0.1724 Beta(编译日期2011-12-05)在Win7+ie9:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
"""
搜狗首页的源码数据爬取
需要UA伪装
"""
import requests
inp = input('搜索:')
params = {
# url携带的请求参数
'query': inp,
}
# UA伪装,请求头信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
# 指定url(网址)
url = f'https://www.sogou.com/web'
# get发起请求,携带请求参数,返回的是一个响应对象
# url 网址,params 参数动态化,headers UA伪装
response = requests.get(url=url, params=params, headers=headers)
response.encoding = 'utf-8' # 手动修改编码格式指定utf8,处理乱码问题
# 获取响应数据 .test
page_text = response.text
fileName = inp + '.html'
# 持久化存储
with open(fileName, 'w', encoding='utf-8') as f:
f.write(page_text)
print('ok')
import requests
# UA伪装,请求头信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
# 请求携带的参数
params = {
'type': '15',
'interval_id': '100:90',
'action': '',
'start': '0',
'limit': '20',
}
# 请求的网址
url = "https://movie.douban.com/j/chart/top_list?"
# 发起请求 url网址 headers请求头 params请求携带的参数
res = requests.get(url=url,params=params,headers=headers)
data_list = res.json() # 返回的是序列化的列表
for i in data_list:
title = i["title"]
types = i["types"]
print(title,types)
import requests
url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
city = input("city name : ")
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
# 全站信息爬取
for i in range(1,111):
# 获取餐厅地址所需要的动态参数,键值对,值为str
data = {
'cname': '',
'pid': '',
'keyword': city, # 数据的搜索地址
'pageIndex': str(i), # 数据的页码数
'pageSize': '10', # 每一页的数据量
}
# headers请求头 data参数动态化
pos_list = requests.post(url=url,headers=headers,data=data).json()["Table1"] # 返回一个响应对象 .json()返回了一个序列化好的字典对象 ['Table1']获取Key所对应的value值
for i in pos_list:
s = i["addressDetail"]
print(s)
requests中,get和post的区别就是参数一个是params,一个是data。
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
url = "http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList"
# 将企业ID存放到列表中
list_1 = []
# 爬取全栈(目前只爬取9页,爬多了之后会被限制)
for a in range(1,10):
data = {
'on': 'true',
'page': str(a),
'pageSize': '15',
'productName': '',
'conditionType': '1',
'applyname': '',
'applysn': '',
}
ret_dic = requests.post(url=url,data=data,headers=headers).json()['list'] # .json()返回一个字典类型的对象 ['list']取字典中list对应的值
# 捕获企业ID
for i in ret_dic:
_id = i['ID']
list_1.append(_id)
detail_url = "http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById"
for i in list_1:
data = {
'id': i
}
com_data = requests.post(url=detail_url,headers=headers,data=data).json()
legalPerson = com_data["legalPerson"]
epsName = com_data["epsName"]
print(legalPerson,epsName)
# 测试了一遍,只爬取了50页,就限制了
标签:列表 加载 数值 好的 查找 types window 初识 shift
原文地址:https://www.cnblogs.com/Golanguage/p/12445009.html