基础1

  • requests:网络请求的模块,模拟浏览器发请求的. pip install requests
    • urllib看下博客<发起请求>
  • 编码流程:
    • 指定url
    • 发起了请求
    • 获取响应数据
    • 持久化存储
 
#爬取搜狗首页的页面源码数据
import requests
#1指定url
url = 'https://www.sogou.com/'
#2发起请求:返回值是一个response
response = requests.get(url=url)
#3.获取响应数据:text返回的字符串形式的响应数据(页面源码数据)
page_text = response.text
#4.持久化存储
with open('sogou.html','w',encoding='utf-8') as fp:
    fp.write(page_text)
 
. . .
 
  • 案例:
    • 简易的网页采集器
    • 爬取豆瓣/肯德基的相关数据
    • 药监总局企业信息的爬取
 
 
 
#简易的网页采集器
url = 'https://www.sogou.com/web'
#设定动态的请求参数
wd = input('enter a key word:')
params = {
    'query':wd
}
#参数2:params是一个字典,是用来处理动态请求参数
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(fileName,'下载成功!')
​
​
#分析:爬取的数据有乱码    爬取的数据有缺失
 
 
 
enter a key word:人民币
人民币.html 下载成功!
 
解决的是乱码问题

 
#简易的网页采集器
url = 'https://www.sogou.com/web'
#设定动态的请求参数
wd = input('enter a key word:')
params = {
    'query':wd
}
#参数2:params是一个字典,是用来处理动态请求参数
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(fileName,'下载成功!')
 
 
 
enter a key word:人民币
人民币.html 下载成功!
 
  • 分析:爬取到的数据显示当前请求为异常的请求,导致我们没有爬取到对应的数据
  • 原因:搜狗对请求载体身份标识做了检测
    • UA检测:搜狗使用的一种反爬机制
    • UA伪装
 
#简易的网页采集器
url = 'https://www.sogou.com/web'
#设定动态的请求参数
wd = input('enter a key word:')
params = {
    'query':wd
}
​
#UA伪装
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
​
#参数2:params是一个字典,是用来处理动态请求参数
response = requests.get(url=url,params=params,headers=headers) #将headers作用的get方法的第三个参数中
#修改原始的响应数据的编码
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(fileName,'下载成功!')
​
 
 
 
enter a key word:人民币
人民币.html 下载成功!
 
In [15]:
 
 
 
 
 
#UA伪装
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
city = input('enter a city name:')
#该字典是用来处理封装post请求的请求参数
data = {
    "cname": '',
    'pid': '',
    'keyword': city,
    'pageIndex': '1',
    'pageSize': '2000',
}
response = requests.post(url=url,data=data,headers=headers)
#json()返回的是序列化之后的json对象.
json_data = response.json()
for dic in json_data['Table1']:
    print(dic['addressDetail'])
 
. . .
 
 
  • 验证我们要爬取的数据,是否为当前页面动态加载出来的?
    • 是:直接爬取不到
    • 不是:可以直接爬取
  • 如何验证页面中的局部数据是否为动态加载呢?
    • 首先在页面中复制一部分的页面内容,然后在通过抓包工具定位到url指定的数据包,在数据包的response中进行刚才复制内容的搜索,搜索到则表示没有动态加载,否则为动态加载!!!
I
 
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
#获取企业的ID
url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList'
for page in range(1,6): #爬取前5页对应的企业ID
    data = {
        'on': 'true',
        'page': str(page),
        'pageSize': '15',
        'productName': '',
        'conditionType': '1',
        'applyname': '',
        'applysn': '',
    }
    print('正在爬取{}的数据......'.format(page))
    json_data = requests.post(url=url,data=data,headers=headers).json()
    for dic in json_data['list']:
        _id = dic['ID']
        #企业详情信息的url需要使用一个固定的域名结合着企业的ID组成
        detail_url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById'
        data = {
            'id':_id
        }
        b_data = requests.post(url=detail_url,headers=headers,data=data).json()
        per_name = b_data['businessPerson']
        company_title = b_data['epsName']
        print(per_name,company_title)
 
 
. . .
 
  • 爬取图片数据
url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564654066124&di=47579504bdd90e51c50c387cbff21391&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201508%2F05%2F20150805214640_tY24E.jpeg'
response = requests.get(url,headers=headers)
img_data = response.content #content返回的是byte类型的数据
with open('美少女.jpg','wb') as fp:
    fp.write(img_data)
 

 
 
 
#基于urllib爬取图片   (缺点 不能UA伪装)
from urllib import request
url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564654066124&di=47579504bdd90e51c50c387cbff21391&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201508%2F05%2F20150805214640_tY24E.jpeg'
request.urlretrieve(url,filename='./meishaonv.jpg')
  • 两种图片爬取方法的差异:
    • 是否可以进行UA伪装
原文地址:https://www.cnblogs.com/qj696/p/11284154.html