python基础之爬虫入门


Posted in Python onMay 10, 2021

前言

python基础爬虫主要针对一些反爬机制较为简单的网站,是对爬虫整个过程的了解与爬虫策略的熟练过程。
爬虫分为四个步骤:请求,解析数据,提取数据,存储数据。本文也会从这四个角度介绍基础爬虫的案例。

一、简单静态网页的爬取

我们要爬取的是一个壁纸网站的所有壁纸

http://www.netbian.com/dongman/

python基础之爬虫入门

1.1 选取爬虫策略——缩略图

首先打开开发者模式,观察网页结构,找到每一张图对应的的图片标签,可以发现我们只要获取到标黄的img标签并向它发送请求就可以得到壁纸的预览图了。

python基础之爬虫入门

随后注意到网站不止一页,打开前3页的网站观察url有没有规律

http://www.netbian.com/dongman/index.htm#第一页
http://www.netbian.com/dongman/index_2.htm#第二页
http://www.netbian.com/dongman/index_3.htm#第三页

我们发现除了第一页其他页数的url都是有着固定规律的,所以先构建一个含有所有页数url的列表

url_start = 'http://www.netbian.com/dongman/'
url_list=['http://www.netbian.com/dongman/index.htm']
if not os.path.exists('./exercise'):
    os.mkdir('./exercise')
for i in range(2,133):
    url = url_start+'index_'+str(i)+'.htm'
    url_list.append(url)

至此我们的基本爬虫策略就确定了。

网页请求

for url in url_list:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text

解析数据

在这里我们选用etree解析数据

tree = etree.HTML(response)

提取数据

在这里我们选用xpath提取数据

leaf = tree.xpath('//div[@class="list"]//ul/li/a/img/@src')
for l in leaf:
      print(l)
      h = requests.get(url=l, headers=headers).content

存储数据

i = 'exercise/' + l.split('/')[-1]
with open(i, 'wb') as fp:
      fp.write(h)

完整代码

import requests
from lxml import etree
import os
url_start = 'http://www.netbian.com/dongman/'
url_list=['http://www.netbian.com/dongman/index.htm']
#http://www.netbian.com/dongman/index_2.htm
if not os.path.exists('./exercise'):
    os.mkdir('./exercise')
for i in range(2,133):
    url = url_start+'index_'+str(i)+'.htm'
    url_list.append(url)
print(url_list)
for url in url_list:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text
    tree = etree.HTML(response)
    leaf = tree.xpath('//div[@class="list"]//ul/li/a/img/@src')
    for l in leaf:
        print(l)
        h = requests.get(url=l, headers=headers).content
        i = 'exercise/' + l.split('/')[-1]
        with open(i, 'wb') as fp:
            fp.write(h)

1.2 选取爬虫策略——高清大图

在刚刚的爬虫中我们爬取到的只是壁纸的缩略图,要想爬到高清版本,就需要我们更改策略。重新打开开发者工具进行观察,发现在原先爬取的img标签之上还有一个href标签,打开之后就会跳转高清大图。

python基础之爬虫入门
python基础之爬虫入门

那么此时我们的爬取策略就变成了提取这个href标签的内容,向这个标签中的网站发送请求,随后在该网站中找到img标签进行再一次请求。

我们用到了正则表达式来提取href标签的内容。正则表达式是比xpath语法更简便的一种数据提取方法,具体有关语法可查看以下文档

for url in url_list:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text
    leaf = re.findall("desk/\d*.htm",response,re.S)
    for l in leaf:
        url = "http://www.netbian.com/"+str(l)
        h = requests.get(url=url, headers=headers).text
        leaf_ =re.findall('<div class="pic">.*?(http://img.netbian.com/file/\d*/\d*/\w*.jpg)',h,re.S)

这样输出的leaf_就是我们要找的高清大图的img标签,此时我们只需要再次发送请求随后再保存数据就可以了。

存储数据

for l_ in leaf_:
      print(l_)
      h = requests.get(url=l_, headers=headers).content
      i = 'exercise/' + l_.split('/')[-1]
      with open(i, 'wb') as fp:
          fp.write(h)

完整代码

import requests
import os
import re
url_start = 'http://www.netbian.com/dongman/'
url_list=['http://www.netbian.com/dongman/index.htm']
if not os.path.exists('./exercise'):
    os.mkdir('./exercise')
for i in range(2,133):
    url = url_start+'index_'+str(i)+'.htm'
    url_list.append(url)
print(url_list)
for url in url_list:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text
    leaf = re.findall("desk/\d*.htm",response,re.S)
    for l in leaf:
        url = "http://www.netbian.com/"+str(l)
        h = requests.get(url=url, headers=headers).text
        leaf_ =re.findall('<div class="pic">.*?(http://img.netbian.com/file/\d*/\d*/\w*.jpg)',h,re.S)
        for l_ in leaf_:
            print(l_)
            h = requests.get(url=l_, headers=headers).content
            i = 'exercise/' + l_.split('/')[-1]
            with open(i, 'wb') as fp:
                fp.write(h)

二、动态加载网站的爬取

我们要爬取的是另一个壁纸网站的所有壁纸

https://sucai.gaoding.com/topic/9080?

python基础之爬虫入门

2.1 选取爬虫策略——selenium

首先打开开发者模式,观察网页结构,此时我们会发现一页上的所有壁纸并不是全部都加载出来了的,也就是说随着我们下拉滚动条,内容会不断实时加载出来,查看网页元素时也能看到lazy-image这个代表动态加载的标签

python基础之爬虫入门

由于是动态加载,因此不能用之前的直接发送请求的办法来爬取数据了,面对这种情况我们就需要模拟浏览器发送一个请求,并且下拉页面,来实现爬取一个实时加载网页的目的。

观察完网页结构之后我们又来观察页数,这次就不多说了,想必大家也能发现规律

url_list=[]
for i in range(1,4):
    url =  'https://sucai.gaoding.com/topic/9080?p={}'.format(i)
    url_list.append(url)

网页请求

在这里我们用到了selenium这个自动化测试框架

for url in url_list:
    driver = webdriver.Chrome()
    driver.get(url)
    driver.maximize_window()
    time.sleep(2)
    i=0
    while i<10:#下拉滚动条加载页面
        i+=1
        driver.execute_script("window.scrollBy(0,500)")
        driver.implicitly_wait(5)#显式等待

解析提取数据

items = driver.find_elements_by_xpath("//*[@class='gdd-lazy-image__img gdd-lazy-image__img--loaded']")
    for item in items:
            href = item.get_attribute('src')
            print(href)

至于数据的存储只需要再请求我们爬下来的href标签的网站就可以了。

完整代码

from selenium import webdriver
import time
import os
if not os.path.exists('./exercise'):
    os.mkdir('./exercise')
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36'
    }
url_list=[]
url_f_list=[]
for i in range(1,4):
    url =  'https://sucai.gaoding.com/topic/9080?p={}'.format(i)
    url_list.append(url)
for url in url_list:
    driver = webdriver.Chrome()
    driver.get(url)
    driver.maximize_window()
    time.sleep(2)
    i=0
    while i<10:
        i+=1
        driver.execute_script("window.scrollBy(0,500)")
        driver.implicitly_wait(5)#显式等待
    items = driver.find_elements_by_xpath("//*[@class='gdd-lazy-image__img gdd-lazy-image__img--loaded']")
    for item in items:
            href = item.get_attribute('src')
            print(href)

2.2 选取爬虫策略——api

众所周知,api接口是个好东西,如果找到了它,我们就无需担心动态加载,请求api返回给我们的是json格式的字典,里面或许有我们需要的东西也说不定。那么我们重新打开开发者工具搜索一番吧!

python基础之爬虫入门

从Element切换到Network我们可以发现这里多了好多奇怪的东西,但是打开preview好像没有我们能用到的。

这个时候别灰心,切换下页面,等第二页加载出来的时候最后又多出来了一个xhr文件,点开preview我们惊喜的发现,这个里面有每一张图id的信息!

python基础之爬虫入门

搜寻一圈发现字典里有效的只有id这个值,那么id对于我们的图片爬取有什么意义呢?通常情况下网址+id就可以定位到具体的图片,于是我点进去一张壁纸,惊喜的发现跟我想的一样!

python基础之爬虫入门

最后又到了我们老生常谈的页数环节,在看到这个api的request url之后大家有没有观察到它其中带着page_num=2&page_size=100这两个看着很像页码的参数呢?我们再往下就看到了参数中也正好有这两个值!也就是说我们只需要更改page_num=2就可以实现翻页了!

python基础之爬虫入门

url='https://api-sucai.gaoding.com/api/csc-api/topics/9080/modules/18928/templets?'
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
params_list=[]
for i in range(1,4):
    parms ={
        'page_num': i,
        'page_size': 100
    }
    params_list.append(parms)

解析提取数据

for param in params_list:
    response = requests.get(url=url,params=param,headers=headers).json()
    for i in range(100):
        try:
            dict  =response[i]
            id = dict['id']
            url_f = 'https://sucai.gaoding.com/material/'+str(id)
            url_f_list.append(url_f)
        except:
            pass

存储数据

for l in url_f_list:
    print(l)
    h = requests.get(url=l, headers=headers).content
    i = 'exercise/' + l.split('/')[-1]
    with open(i, 'wb') as fp:
        fp.write(h)

完整代码

import os
import requests
if not os.path.exists('./exercise'):
    os.mkdir('./exercise')
url='https://api-sucai.gaoding.com/api/csc-api/topics/9080/modules/18928/templets?'
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
params_list=[]
url_f_list=[]
for i in range(1,4):
    parms ={
        'page_num': i,
        'page_size': 100
    }
    params_list.append(parms)
for param in params_list:
    response = requests.get(url=url,params=param,headers=headers).json()
    for i in range(100):
        try:
            dict  =response[i]
            id = dict['id']
            url_f = 'https://sucai.gaoding.com/material/'+str(id)
            url_f_list.append(url_f)
        except:
            pass
for l in url_f_list:
    print(l)
    #h = requests.get(url=l, headers=headers).content
    #i = 'exercise/' + l.split('/')[-1]
    #with open(i, 'wb') as fp:
    #    fp.write(h)

三、selenium模拟登录

我们要爬取的网站总是免不了登录这一关键环节,因此模拟登录也是一大爬虫基础。
我们要模拟登录的网站如下

https://www.icourse163.org/course/BIT-268001

python基础之爬虫入门

选取爬虫策略

既然我们是用selenium模拟登陆,首先肯定要明确我们要模拟的具体内容,归纳起来就是

点击 登录|注册
点击 其他登陆方式
点击 手机号登录
输入账号
输入密码
点击 登录

在明确该干些什么之后我们就打开开发者模式观察一下这个登录框吧。

python基础之爬虫入门

不看不知道,一看吓一跳,原来这里有一个iframe框架,这就意味着如果我们不做任何处理就查找元素的话可能会什么都查找不到。这就相当于在王家找李家的东西一样,我们首先需要切换到当前iframe

driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="j-ursContainer-1"]/iframe'))

经过这一操作之后我们就可以正常按部就班的进行模拟登陆了!

完整代码

from selenium import webdriver
import time
url = 'https://www.icourse163.org/course/BIT-268001'
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
#time.sleep(2)
driver.find_element_by_xpath('//div[@class="unlogin"]/a').click()
driver.find_element_by_class_name('ux-login-set-scan-code_ft_back').click()
driver.find_element_by_xpath('//ul[@class="ux-tabs-underline_hd"]/li[2]').click()
driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="j-ursContainer-1"]/iframe'))
driver.implicitly_wait(2)#给登录框一些加载的时间
driver.find_element_by_css_selector('input[type="tel"]').send_keys('15201359153')
driver.find_element_by_css_selector('input[class="j-inputtext dlemail"]').send_keys('Asdasd123')
driver.implicitly_wait(2)#如果不等待的话可能密码还没输入结束就点按登录键了
driver.find_element_by_id('submitBtn').click()

到此这篇关于python基础之爬虫入门的文章就介绍到这了,更多相关python入门爬虫内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python虚拟环境virtualenv的使用教程
Oct 20 Python
python 图像平移和旋转的实例
Jan 10 Python
Python实现去除图片中指定颜色的像素功能示例
Apr 13 Python
使用python实现离散时间傅里叶变换的方法
Sep 02 Python
Python3 使用map()批量的转换数据类型,如str转float的实现
Nov 29 Python
Python3爬虫带上cookie的实例代码
Jul 28 Python
python中append函数用法讲解
Dec 11 Python
python Autopep8实现按PEP8风格自动排版Python代码
Mar 02 Python
Python批量将csv文件转化成xml文件的实例
May 10 Python
解决pytorch读取自制数据集出现过的问题
May 31 Python
用Python创建简易网站图文教程
Jun 11 Python
python playwrigh框架入门安装使用
Jul 23 Python
python设置 matplotlib 正确显示中文的四种方式
提取视频中的音频 Python只需要三行代码!
Python-typing: 类型标注与支持 Any类型详解
May 10 #Python
超详细Python解释器新手安装教程
Python机器学习三大件之一numpy
python实现自动清理文件夹旧文件
May 10 #Python
Python中的min及返回最小值索引的操作
May 10 #Python
You might like
JavaScript window.setTimeout() 的详细用法
2009/11/04 Javascript
跨浏览器的事件对象介绍
2012/06/27 Javascript
Javascript控制页面链接在新窗口打开具体方法
2013/08/16 Javascript
JavaScript原生对象之String对象的属性和方法详解
2015/03/13 Javascript
聊一聊JavaScript作用域和作用域链
2016/05/03 Javascript
使用jquery实现的循环连续可停顿滚动实例
2016/11/23 Javascript
微信小程序 出现错误:{&quot;baseresponse&quot;:{&quot;errcode&quot;:-80002,&quot;errmsg&quot;:&quot;&quot;}}解决办法
2017/02/23 Javascript
AngularJS实现的自定义过滤器简单示例
2019/02/02 Javascript
关于angular 8.1使用过程中的一些记录
2020/11/25 Javascript
[01:24:51]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS NewBee第二场
2014/05/26 DOTA
[01:12]DOTA2次级职业联赛 - Newbee.Y 战队宣传片
2014/12/01 DOTA
Python Web框架Flask信号机制(signals)介绍
2015/01/01 Python
使用python实现rsa算法代码
2016/02/17 Python
Python多进程库multiprocessing中进程池Pool类的使用详解
2017/11/24 Python
使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法
2018/06/22 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
python 函数的缺省参数使用注意事项分析
2019/09/17 Python
tensorflow通过模型文件,使用tensorboard查看其模型图Graph方式
2020/01/23 Python
python+OpenCV实现图像拼接
2020/03/05 Python
html5读取本地文件示例代码
2014/04/22 HTML / CSS
美国一家主营日韩美妆护肤品的在线商店:iMomoko
2016/09/11 全球购物
Columbia美国官网:美国著名的户外服装品牌
2016/11/24 全球购物
CHARLES & KEITH台湾官网:新加坡时尚品牌
2019/07/30 全球购物
介绍一下.NET构架下remoting和webservice
2014/05/08 面试题
年终奖发放方案
2014/06/02 职场文书
医院深入开展党的群众路线教育实践活动实施方案
2014/08/27 职场文书
纪念9.18事变演讲稿
2014/09/14 职场文书
党的群众路线教育实践活动剖析材料
2014/09/30 职场文书
2014年客户经理工作总结
2014/11/20 职场文书
2015年公司新年寄语
2014/12/08 职场文书
趣味运动会口号
2015/12/24 职场文书
2016暑期校本培训心得体会
2016/01/08 职场文书
如何用python插入独创性声明
2021/03/31 Python
解析Redis Cluster原理
2021/06/21 Redis
java如何实现socket连接方法封装
2021/09/25 Java/Android
科学家研发出新型速效酶,可在 24 小时内降解塑料制品
2022/04/29 数码科技