Python学习笔记之十(爬虫进阶2)

Python学习笔记之十(爬虫进阶2)

2019-07-22 09:44:35 星期一

本课概要
  • 作业讲解
  • 抓包分析概述
  • 使用Fiddler进行抓包分析
  • 抓取HTTPS数据包
  • 爬取腾讯视频的评论
抓包分析概述

所谓抓包分析,即将网络传输发送与接收的数据包进行抓取的操作,做爬虫时,数据并不一定就在HTML源码中,很可能隐藏在一些网址中,所以,我们要抓取某些数据,就需要进行抓包,分析出对应数据所隐藏在的网址,然后分析规律并爬取。

使用Fiddler进行抓包分析

利用Fiddler获取腾讯视频评论信息的url

略(证书认证失败)

微信爬虫

什么是微信爬虫

所谓微信爬虫,及自动获取微信的相关文章信息的一种爬虫。微信对我们的限制是很多的,所以,我们需要采取一些手段解决这些限制,主要包括伪装浏览器、使用代理IP等方式。

#微信爬虫实战
import re
import urllib.request
import time
import urllib.error
#自定义函数,功能是使用代理服务器爬一个网址
def use_proxy(proxy_addr,url):
    try:
        req=urllib.request.Request(url)
        req.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
        proxy=urllib.request.ProxyHandler({'http':proxy_addr})
        opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
        urllib.request.install_opener(opener)
        data=urllib.request.urlopen(req).read()
        return data
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    except Exception as e:
        print("exception:"+str(e))
        #若exception异常,延时1s执行
        time.sleep(1)      
#设置关键词
key="python"
#设置代理服务器
proxy="127.0.0.1:8888"
#爬多少页信息
for i in range(0,5):
    key=urllib.request.quote(key)
    thispageurl="https://weixin.sogou.com/weixin?type=2&s_from=input&query="+key+"&page="+str(i)
    print(thispageurl)
    #a="http://blog.csdn.net"
    thispagedata=use_proxy(proxy,thispageurl)
    print(len(str(thispagedata)))
    pat1='<a href="(.*?)"'
    rs1=re.compile(pat1,re.S).findall(str(thispagedata))
    if(len(rs1)):
        print("此次("+str(i)+"页)没成功")
        continue
    for j in range(0,len(rs1)):
        thisurl=rs1[j]
        thisurl=thisurl.replace("amp;","")
        print(thisurl)
        file="F:/PL/weixinInf"+str(i)+"页第"+str(j)+"篇文章.html"
        thisdata=use_proxy(proxy,thisurl)
        try:
            fh=open(file,"wb")
            fh.write(thisdata)
            fh.close
            print("第"+str(i)+"页第"+str(j)+"篇文章成功")
        except Exception as e:
            print(e)
            print("第"+str(i)+"页第"+str(j)+"篇文章失败")

#csdn博客爬虫实战
import re
import urllib.request
import time
import urllib.error
#自定义函数,功能是使用代理服务器爬一个网址
def use_proxy(proxy_addr,url):
    try:
        req=urllib.request.Request(url)
        req.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
        proxy=urllib.request.ProxyHandler({'http':proxy_addr})
        opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
        urllib.request.install_opener(opener)
        data=urllib.request.urlopen(req).read()
        return data
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    except Exception as e:
        print("exception:"+str(e))
        #若exception异常,延时1s执行
        time.sleep(1)      
#设置关键词
key="python"
#设置代理服务器
proxy="127.0.0.1:8888"
#爬多少页信息
for i in range(0,3):
    key=urllib.request.quote(key)
    #thispageurl="https://weixin.sogou.com/weixin?type=2&s_from=input&query="+key+"&page="+str(i)
    thispageurl="https://so.csdn.net/so/search/s.do?p="+str(i)+"&q="+key+"&t=blog&domain"
    print(thispageurl)
    #a="http://blog.csdn.net"
    thispagedata=use_proxy(proxy,thispageurl)
    print(len(str(thispagedata)))
    pat1='<a href="(.*?)"'
    rs1=re.compile(pat1,re.S).findall(str(thispagedata))
    if(len(rs1)==0):
        print("此次("+str(i)+"页)没成功")
        continue
    for j in range(0,len(rs1)):
        thisurl=rs1[j]
        #thisurl=thisurl.replace("amp;","")
        #print(thisurl)
        file="F:/PL/weixinInf/"+str(i)+"页第"+str(j)+"篇文章.html"
        thisdata=use_proxy(proxy,thisurl)
        try:
            fh=open(file,"wb")
            fh.write(thisdata)
            fh.close
            print("第"+str(i)+"页第"+str(j)+"篇文章成功")
        except Exception as e:
            print(e)
            print("第"+str(i)+"页第"+str(j)+"篇文章失败")

多线程爬虫

本课概要
  • 什么是多线程爬虫
  • 糗事百科段子爬虫
  • 将糗事百科段子爬虫改造为多线程爬虫
什么是多线程爬虫

所谓多线程,即程序中的某些程序段并行执行,合理地设置多线程,可以让爬虫效率更高。

#多线程爬虫
#传统爬虫
import re
import urllib.request
import time
import urllib.error
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)
#data=urllib.request.urlopen(req).read()
for i in range(1,4):
    url="https://www.qiushibaike.com/8hr/page/"+str(i)
    pagedata=urllib.request.urlopen(url).read().decode("utf-8","ignore")
    pat='<div class="recmd-right">.*?<a class="recmd-content" href=.*?>(.*?)</a>'
    #pat='<div class="content">.*?<span>(.*?)</span>.*?</div>'
    datalist=re.compile(pat,re.S).findall(pagedata)
    for j in range (0,len(datalist)):
        print("第"+str(i)+"页第"+str(j)+"个段子的内容是:")
        print(datalist[j])

#将传统爬虫改造为多线程爬虫
#线程入门
import threading
class A(threading.Thread):#建立一个线程
    def _init_(self): #默认参数self
        threading.Thread._init_(self)# 初始化线程
    def run(self):
        for i in range (0,10):
            print("我是线程A")
class B(threading.Thread):
    def _init_(self): 
        threading.Thread._init_(self)
    def run(self):
        for j in range (0,10):
            print("我是线程B")
#实例化线程
t1=A()
t1.start()
t2=B()
t2.start()

#将传统爬虫改造为多线程爬虫
import threading
import re
import urllib.request
import time
import urllib.error
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)
#data=urllib.request.urlopen(req).read()
class One(threading.Thread):
    def _init_(self): #默认参数self
        threading.Thread._init_(self)# 初始化线程
    def run(self):
        for i in range (1,36,2):
            url="https://www.qiushibaike.com/8hr/page/"+str(i)
            pagedata=urllib.request.urlopen(url).read().decode("utf-8","ignore")
            pat='<div class="recmd-right">.*?<a class="recmd-content" href=.*?>(.*?)</a>'
            #pat='<div class="content">.*?<span>(.*?)</span>.*?</div>'
            datalist=re.compile(pat,re.S).findall(pagedata)
            for j in range (0,len(datalist)):
                print("第"+str(i)+"页第"+str(j)+"个段子的内容是:")
                print(datalist[j])
class Two(threading.Thread):
    def _init_(self): #默认参数self
        threading.Thread._init_(self)# 初始化线程
    def run(self):
        for i in range (0,36,2):
            url="https://www.qiushibaike.com/8hr/page/"+str(i)
            pagedata=urllib.request.urlopen(url).read().decode("utf-8","ignore")
            pat='<div class="recmd-right">.*?<a class="recmd-content" href=.*?>(.*?)</a>'
            #pat='<div class="content">.*?<span>(.*?)</span>.*?</div>'
            datalist=re.compile(pat,re.S).findall(pagedata)
            for j in range (0,len(datalist)):
                print("第"+str(i)+"页第"+str(j)+"个段子的内容是:")
                print(datalist[j])
one=One()
one.start()
two=Two()
two.start()

scrapy框架的安装

本课概要
  • 什么是Scrapy框架
  • 安装Scrapy框架及各种常见错误解决技巧
  • 少坑版安装方式
  • 常见错误与解决
  • 作业
什么是Scrapy框架

Scrapy是一个Python爬虫框架,非常适合做一些大型爬虫项目,并且开发者利用这个框架,可以不用过多关注细节,我们会重点说这个框架。
Scrapy的官网地址是:http://scrapy.org/

少坑版安装方式

由于Scrapy框架涉及太多依赖库,在此,如果想省事的朋友,可以按照这种方式去安装。女解具体过程的朋友,可以先安装pip install scrapy,然后遇到错误参加后面的方式解决。
接下来讲解少坑安装方式。
0.开个VPN或者采用本地安装方式
1.首先,升级pip:
python-m pip install–upgrade pip
2、安装Visual Studio 2015专业版,去http://download.microsoft.com/download/B/8/9/B898E46E-CBAE-4045-A8E2-
2D33DD36F3C4/vs2015.pro_chs.iso下载
3、安装lxml
http://www.lfd.uci.edu/~gohlke/pythonlibs/
找"lxml-"、pip install wheel、pip install lxml-3.6.4-cp35-cp35m-win amd64.whl
4、pip install scrapy或pip install scrapy==1.1.0rc3

安装成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫进阶涉及到一些高级技术和技巧,以下是一些你可以学习和探索的主题: 1. 多线程和多进程:使用多线程或多进程可以提高爬虫的效率,同时处理多个请求或任务。 2. 使用代理:在爬取网页时,你可能会被网站封禁IP,使用代理可以轮流切换IP地址来规避封禁。 3. 反反爬虫策略:有些网站会采取一些手段防止爬虫,你需要学习如何识别和应对这些策略,比如验证码、页面解密等。 4. 使用Cookie和Session:有些网站会使用Cookie和Session来验证用户身份,你需要学习如何在爬虫中模拟登录和保持会话状态。 5. 使用数据库:将爬取到的数据存储到数据库中可以方便后续的数据分析和处理。 6. 使用框架和库:学习使用一些流行的爬虫框架和库,比如Scrapy、BeautifulSoup、Requests等,可以大大简化爬虫的开发和管理。 7. 高级数据解析和提取:学习使用正则表达式、XPath、CSS选择器等高级技术来解析和提取网页中的数据。 8. 动态网页爬取:学习使用Selenium等工具来爬取动态生成的网页内容,比如通过JavaScript异步加载的数据。 9. 分布式爬虫学习如何构建分布式爬虫系统,可以提高爬取效率和可靠性。 10. 爬虫的合法性和道德问题:学习了解相关法律法规和伦理道德,确保你的爬虫行为合法合规。 这些都是Python爬虫进阶的一些方向,你可以根据自己的兴趣和需求选择学习的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值