zoukankan      html  css  js  c++  java
  • 爬虫

    目录

    爬虫

    1 爬虫简介

    爬虫就是通过代码模拟人,向浏览器发送请求(使用模块:requests,selenium),
    获取到网页前端代码后通过筛选,提取出有用的数据(使用模块:bs4,xpath,re)
    最后将数据存放于数据库或文件中(文件,excel,mysql,redis,mongodb)
    

    爬虫协议

    robots.txt (写了允许爬的路由)

    如:https://www.cnblogs.com/robots.txt

    2 爬虫的流程

    # 1、发起请求
    使用http库向目标站点发起请求,即发送一个Request
    Request包含:请求头、请求体、请求地址(浏览器调试,抓包工具),请求头(难),请求体(难),请求方法
    
    # 2、获取响应内容
    如果服务器能正常响应,则会得到一个Response
    Response包含:html,xml,json,图片,视频,经过加密的未知格式(需要解密)等
    
    # 3、解析内容
    解析html数据:正则表达式,第三方解析库如Beautifulsoup,pyquery等
    解析json数据:json模块
    解析二进制数据:以b的方式写入文件
    
    # 4、保存数据
    数据库:推荐使用Mongodb(存json格式数据)
    文件
    
    # 5、提高效率
    开启多进程,多线程,协程
    

    3 requests模块

    安装:pip3 install requests
    

    3.1 requests模块简介

    # 介绍:
    使用requests可以模拟浏览器的请求,比起urllib,requests模块的api更加便捷(本质就是封装了urllib3)
    
    
    # 注意点:
    requests库发送请求将网页内容下载下来以后,并不会执行js代码,
    需要分析目标站点然后手动发起新的request请求
    
    
    # 各种请求方式:常用的是get和post
    import requests
    r = requests.get('https://api.github.com/events')
    r = requests.post('http://httpbin.org/post', data = {'key':'value'})
    r = requests.put('http://httpbin.org/put', data = {'key':'value'})
    r = requests.delete('http://httpbin.org/delete')
    r = requests.head('http://httpbin.org/get')
    r = requests.options('http://httpbin.org/get')
    
    
    # 建议在正式学习requests前,先熟悉下HTTP协议
    http://www.cnblogs.com/linhaifeng/p/6266327.html
    

    3.2 requests的使用

    3.2.1 基本请求

    import requests
    response=requests.get('http://dig.chouti.com/')
    print(response.text)
    

    3.2.2 带参数的请求

    3.2.2.1 带请求头参数的方法

    # 对百度发请求,携带查询参数:wd=python
    
    # 在请求头内将自己伪装成浏览器,使用浏览器的User-Agent,该参数能显示请求的来源,否则百度不会正常返回页面内容,即在请求头内添加User-Agent
    'User-Agent':'Mozilla/5.0 ...'
    
    
    import requests
    response=requests.get('https://www.baidu.com/s?wd=python',headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'})
    print(response.text)
    
    
    # 请求头中较重要的参数
    
    Host
    Referer # 大型网站通常都会根据该参数判断请求的来源(一般检查是否为本站)
    User-Agent # 客户端(请求来源的客户端)
    Cookie	
    

    3.2.2.2 第一种带查询参数的方法(不推荐)

    # 第一种带参数的方法:直接在url地址后使用?&=进行拼接,如?name=aaa&age=18
    # 使用第一种如果查询关键词中有中文或者有其他特殊符号,可能会报错,所以需要将中文进行url编码
    from urllib.parse import urlencode,unquote
    # urlencode编码,unquote解码
    
    print(urlencode({'wd':'哈哈哈'}))
    # wd=%E5%93%88%E5%93%88%E5%93%88
    
    print(unquote('wd=%E5%93%88%E5%93%88%E5%93%88'))
    # wd=哈哈哈
    
    import requests
    header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'}
    
    res=requests.get('https://www.baidu.com/s?wd=%E5%93%88%E5%93%88%E5%93%88',headers=header)
    

    3.2.2.3 第二种带查询参数的方法(推荐)

    import requests
    header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'}
    
    res=requests.get('https://www.baidu.com/s',headers=header,params={'wd':'哈哈哈'})
    # 使用params参数,传入字典即可,自动转换为url编码,无需手动转换
    

    3.2.2.4 手动带cookie参数的两种方法

    # 登录github,然后从浏览器中获取cookies,就可以直接使用cookie登录了,无需输入用户名密码
    # 用户名:egonlin 邮箱378533872@qq.com 密码lhf@123
    
    import requests
    # 第一次请求  登录
    res=session.post('https://github.com/session',data={"username":"wu",'password':'123'})
    # 取出cookie信息
    Cookies={'user_session':'wGMHFJKgDcmRIVvcA14_Wrt_3xaUyJNsBnPbYzEL6L0bHcfc'}
    # 下次发送请求时手动携带,传入cookies,cookies是一个字典或者CookieJar对象
    response=requests.get('https://github.com/settings/emails',cookies=Cookies)
    # 也可以手动写入headers中
    response=requests.get('https://github.com/settings/emails',headers={"Cookie":Cookies})
    
    # github对请求头没有限制,无需定制user-agent,对于其他网站可能需要定制
    
    print('378533872@qq.com' in response.text) #True
    

    3.2.2.5 自动带cookie参数的方法

    import requests
    
    session=requests.session()
    # 第一次请求  登录
    res=session.post('https://github.com/session',data={"username":"wu",'password':'123'})	
    # cookie信息会自动存入res,下次发送请求自动携带cookie信息
    res2=session.get('https://github.com/settings/emails')
    

    3.2.2.6 带请求体的方法

    # 携带数据:urlencoded
    res=requests.post('http://127.0.0.1:8000/index/',data={'name':'wu'})
    print(res.text)
    
    # 携带数据:json
    res=requests.post('http://127.0.0.1:8000/index/',json={'age':1,},)
    print(res.text)
    

    3.2.3 请求对象的属性

    res=requests.post('http://127.0.0.1:8000/index/',data={'name':'wu'})
    
    print(respone.text)  # 响应的文本
    
    print(respone.content)  # 响应体的二进制
    
    print(respone.status_code)  # 响应状态码
    
    print(respone.headers)    # 响应头
    
    print(respone.cookies)   # cookie
    
    print(respone.cookies.get_dict()) # 把cookie转成字典
    
    print(respone.cookies.items())  # [(k1,v1),(k2,v2),(k3,v3)]
    
    print(respone.url)        # 请求的url
    
    print(respone.history)   # []内为重定向之前的历史记录响应对象
    
    print(respone.encoding)  # 响应的编码方式
    
    print(respone.iter_content())  # 视频,大文件,可以循环取出来
    for line in respone.iter_content():
        f.write(line)
    
    

    3.2.4 页面乱码

    # 页面乱码问题解决方案
    res=requests.get('http://www.autohome.com/news')
    # 方式一:指定解码方式
    res.encoding='gb2312'
    # 方式二:自动解码
    res.encoding=res.apparent_encoding
    

    3.2.5 解析json格式

    import json
    
    respone=requests.post('http://127.0.0.1:8000/index/',data={'name':'lqz'})
    print(type(respone.text))  # 响应的文本
    # 手动序列化
    print(json.loads(respone.text))
    # 内置的序列化方法
    print(respone.json())  # 相当于上面那句话
    

    3.2.6 ssl(了解)

    import requests
    
    # 对于需要证书的网址,直接访问会报警告,返回200
    respone=requests.get('https://www.12306.cn')
    print(respone.status_code)
    
    # 使用证书,需要手动携带
    import requests
    
    respone=requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
    print(respone.status_code)
    

    3.2.7 代理

    import requests
    
    respone=requests.get('http://127.0.0.1:8000/index/',proxies={'http':'代理的地址和端口号',})
    
    # 代理池:列表放了若干代理ip,每次随机取一个
    
    # 高匿代理与透明代理:如果使用高匿代理,后端一般无法获取你的ip,使用透明代理,后端能够直接获取到你的ip
    # 后端如何拿到透明代理的ip:  后端:X-Forwarded-For
    
    respone=requests.get('https://www.baidu.com/',proxies={'http':'27.46.20.226:8888'})
    print(respone.text)
    

    3.2.8 设置超时时间

    import requests
    respone=requests.get('https://www.baidu.com',timeout=0.0001)
    

    3.2.9 认证设置(了解)

    import requests
    
    r=requests.get('xxx',auth=('user','password'))
    # 网页弹出登录框,现在已经基本绝迹
    print(r.status_code)
    
    

    3.2.10 异常处理

    import requests
    from requests.exceptions import * 
    #可以查看requests.exceptions获取异常类型
    
    try:
        r=requests.get('http://www.baidu.com',timeout=0.00001)
    except Exception as e:
        print(e)
    

    3.2.11 上传文件

    import requests
    
    res=requests.post('http://127.0.0.1:8000/index/',files={'myfile':open('a.jpg','rb')})
    # files传字典,value为文件对象
    print(res.text)
    

    3.3 模拟登陆某网站

    # http://www.aa7a.cn/
    import requests
    session=requests.session()
    data = {
        'username': '616564099@qq.com',
        'password': 'lqz123',
        'captcha': 'xxxx',		# 验证码
        'remember': 1,		# 是否记住密码
        'ref': 'http://www.aa7a.cn/user.php?act=logout',	# referer
        'act': 'act_login',		# 操作
    }
    rest = session.post('http://www.aa7a.cn/user.php',data=data)
    print(rest.text)
    # 拿到cookie
    cookie=rest.cookies
    print(cookie)
    
    # 携带着cookies,表示登录了,页面中会有我们的用户信息616564099@qq.com
    rest1=session.get('http://www.aa7a.cn/index.php')
    # rest1=requests.get('http://www.aa7a.cn/index.php')
    print('616564099@qq.com' in rest1.text)
    

    3.4 爬取梨视频

    # https://www.pearvideo.com/
    import requests
    import re
    
    res=requests.get('https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0')
    
    
    re_video='<a href="(.*?)" class="vervideo-lilink actplay">'
    video_urls=re.findall(re_video,res.text)
    
    for video in video_urls:
        url='https://www.pearvideo.com/'+video
        print(url)
        # 向视频详情发送get请求
        res_video=requests.get(url)
        # print(res_video.text)
        re_video_mp4='hdUrl="",sdUrl="",ldUrl="",srcUrl="(.*?)",vdoUrl=srcUrl,skinRes'
        video_url=re.findall(re_video_mp4,res_video.text)[0]
        print(video_url)
        video_name=video_url.rsplit('/',1)[-1]
        print(video_name)
        res_video_content=requests.get(video_url)
        with open(video_name,'wb') as f:
            for line in res_video_content.iter_content():
                f.write(line)
    

    4 beautifulsoup4模块

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4

    pip3 install beautifulsoup4  
    用于解析/修改html和xml
    
    #安装 Beautiful Soup
    pip install beautifulsoup4
    
    #安装解析器
    Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml .根据操作系统不同,可以选择下列方法来安装lxml:
    
    $ apt-get install Python-lxml
    $ easy_install lxml
    $ pip install lxml
    
    另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:
    
    $ apt-get install Python-html5lib
    $ easy_install html5lib
    $ pip install html5lib
    
    解析器 使用方法 优势 劣势
    Python标准库 BeautifulSoup(markup, "html.parser") Python的内置标准库执行速度适中文档容错能力强 Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
    lxml HTML 解析器 BeautifulSoup(markup, "lxml") 速度快文档容错能力强 需要安装C语言库
    lxml XML 解析器 BeautifulSoup(markup, ["lxml", "xml"])``BeautifulSoup(markup, "xml") 速度快唯一支持XML的解析器 需要安装C语言库
    html5lib BeautifulSoup(markup, "html5lib") 最好的容错性以浏览器的方式解析文档生成HTML5格式的文档 速度慢不依赖外部扩展

    4.1 爬取汽车之家新闻

    import requests
    from bs4 import BeautifulSoup
    
    
    res=requests.get('https://www.autohome.com.cn/news/1/#liststart')
    # print(res.text)
    
    # 第二个参数是解析器类型
    
    # html.parser是python内置的,不需要安装
    soup=BeautifulSoup(res.text,'html.parser')
    
    # 安装lxml解析器:pip3 install lxml
    soup=BeautifulSoup(res.text,'lxml')
    
    # 查找class为article-wrapper的div
    div=soup.find(class_='article-wrapper')
    div=soup.find(id='auto-channel-lazyload-article')
    print(div)
    
    ul=soup.find(class_='article')
    print(ul)
    
    
    # 继续找ul下的s所有li
    li_list=ul.find_all(name='li')
    print(len(li_list))
    
    
    for li in li_list:
        # 找li中的内容
        title=li.find(name='h3')
        if title:
            title=title.text
            # url=li.find('a')['href']
            url='https:'+li.find('a').attrs.get('href')
            desc=li.find('p').text
            img='https:'+li.find(name='img').get('src')
            print('''
            新闻标题:%s
            新闻地址:%s
            新闻摘要:%s
            新闻图片:%s
            '''%(title,url,desc,img))
    
    

    4.2 bs4的使用

    4.2.1 基本使用演示

    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title"><b>The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """
    
    #基本使用:容错处理,文档的容错能力指的是在html代码不完整的情况下,使用该模块可以识别该错误。使用BeautifulSoup解析上述代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html_doc,'lxml') #具有容错功能
    res=soup.prettify() #处理好缩进,结构化显示
    print(res)
    

    4.2.2 遍历文档树

    #遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
    #1、用法
    #2、获取标签的名称
    #3、获取标签的属性
    #4、获取标签的内容
    #5、嵌套选择
    #6、子节点、子孙节点
    #7、父节点、祖先节点
    #8、兄弟节点
    
    #遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """
    
    #1、用法
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html_doc,'lxml')
    # soup=BeautifulSoup(open('a.html'),'lxml')
    
    print(soup.p) #存在多个相同的标签则只返回第一个
    print(soup.a) #存在多个相同的标签则只返回第一个
    
    #2、获取标签的名称
    print(soup.p.name)
    
    #3、获取标签的属性
    print(soup.p.attrs)
    p=soup.body.p
    # class可能有多个,即便有一个也放到列表中
    print(p.attrs)
    print(p.attrs.get('class'))
    print(p['class'])
    print(p.get('class'))
    
    #4、获取标签的内容
    print(soup.p.string) # p下的文本只有一个时,取到,否则为None
    print(soup.p.strings) # 拿到一个生成器对象, 取到p下所有的文本内容
    print(soup.p.text) # 取到p下所有的文本内容
    for line in soup.stripped_strings: # 去掉空白
        print(line)
    
    '''
    如果tag包含了多个子节点,tag就无法确定 .string 方法应该调用哪个子节点的内容, .string 的输出结果是 None,如果只有一个子节点那么就输出该子节点的文本,比如下面的这种结构,soup.p.string 返回为None,但soup.p.strings就可以找到所有文本
    <p id='list-1'>
        哈哈哈哈
        <a class='sss'>
            <span>
                <h1>aaaa</h1>
            </span>
        </a>
        <b>bbbbb</b>
    </p>
    '''
    
    # 5、嵌套选择
    print(soup.head.title.string)
    print(soup.body.a.string)
    
    # 6、子节点、子孙节点
    print(soup.p.contents) # p下所有子节点
    print(soup.p.children) # 得到一个迭代器,包含p下所有子节点
    
    for i,child in enumerate(soup.p.children):
        print(i,child)
    
    print(soup.p.descendants) # 获取子孙节点,p下所有的标签都会选择出来
    for i,child in enumerate(soup.p.descendants):
        print(i,child)
    
    # 7、父节点、祖先节点
    print(soup.a.parent) # 获取a标签的父节点
    print(soup.a.parents) # 找到a标签所有的祖先节点,父亲的父亲,父亲的父亲的父亲...
    
    # 8、兄弟节点
    print('=====>')
    print(soup.a.next_sibling) # 下一个兄弟
    print(soup.a.previous_sibling) # 上一个兄弟
    
    print(list(soup.a.next_siblings)) # 下面的兄弟们=>生成器对象
    print(soup.a.previous_siblings) # 上面的兄弟们=>生成器对象
    

    4.2.3 搜索文档树

    4.2.3.1 过滤器

    搜索文档树:BeautifulSoup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似
    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b>
    </p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """
    
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html_doc,'lxml')
    
    #五种过滤器: 字符串、正则表达式、列表、bool、方法
    
    # 字符串:即标签名
    print(soup.find_all('b'))
    print(soup.find(href='http://example.com/elsie'))
    print(soup.find(attrs={'id':'my_p'}))
    
    
    # 正则表达式
    import re
    print(soup.find_all(re.compile('^b'))) #找出b开头的标签,结果有body和b标签
    
    
    # 列表:如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签:
    print(soup.find_all(['a','b']))
    print(soup.find_all(class_=['sister','title']))
    
    
    # bool:可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点
    print(soup.find_all(id=False))
    print(soup.find_all(href=True))
        
    # 方法(了解):如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 ,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False
    def has_class_but_no_id(tag):
        return tag.has_attr('class') and not tag.has_attr('id')
    
    print(soup.find_all(has_class_but_no_id))
    
    
    
    
    
    # bs4的修改文档树  软件配置文件是xml格式的
    
    # 软件的配置文件
    # ini:configparser
    # conf
    # xml:bs4
    # yaml格式
    
    

    4.2.3.2 find_all

    # find_all( name , attrs , recursive , text , **kwargs )
    
    
    # name: 搜索name参数的值可以使任一类型的 过滤器 ,字符窜,正则表达式,列表,方法或是 True .
    print(soup.find_all(name=re.compile('^t')))
    
    # keyword: key=value的形式,value可以是过滤器:字符串 , 正则表达式 , 列表, True .
    print(soup.find_all(id=re.compile('my')))
    print(soup.find_all(href=re.compile('lacie'),id=re.compile('d'))) #注意类要用class_
    print(soup.find_all(id=True)) #查找有id属性的标签
    
    # 有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性:
    data_soup = BeautifulSoup('<div data-foo="value">foo!</div>','lxml')
    # data_soup.find_all(data-foo="value") #报错:SyntaxError: keyword can't be an expression
    # 但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag:
    print(data_soup.find_all(attrs={"data-foo": "value"}))
    # [<div data-foo="value">foo!</div>]
    
    # 按照类名查找,注意关键字是class_,class_=value,value可以是五种选择器之一
    print(soup.find_all('a',class_='sister')) #查找类为sister的a标签
    print(soup.find_all('a',class_='sister ssss')) #查找类为sister和sss的a标签,顺序错误也匹配不成功
    print(soup.find_all(class_=re.compile('^sis'))) #查找类为sister的所有标签
    
    # attrs
    print(soup.find_all('p',attrs={'class':'story'}))
    
    # text: 值可以是:字符,列表,True,正则
    print(soup.find_all(text='Elsie'))
    print(soup.find_all('a',text='Elsie'))
    
    # limit参数:如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果
    print(soup.find_all('a',limit=2))
    
    # recursive:调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False.即只查找第一层
    print(soup.html.find_all('a'))
    print(soup.html.find_all('a',recursive=False))
    
    '''
    像调用 find_all() 一样调用tag
    find_all() 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 find_all() 方法相同,下面两行代码是等价的:
    soup.find_all("a")
    soup("a")
    这两行代码也是等价的:
    soup.title.find_all(text=True)
    soup.title(text=True)
    '''
    

    4.2.3.3 find

    # find( name , attrs , recursive , text , **kwargs )
    
    
    find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个<body>标签,那么使用 find_all() 方法来查找<body>标签就不太合适, 使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:
    
    soup.find_all('title', limit=1)
    # [<title>The Dormouse's story</title>]
    soup.find('title')
    # <title>The Dormouse's story</title>
    
    唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.
    find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .
    print(soup.find("nosuchtag"))
    # None
    
    soup.head.title 是 tag的名字 方法的简写.这个简写的原理就是多次调用当前tag的 find() 方法:
    
    soup.head.title
    # <title>The Dormouse's story</title>
    soup.find("head").find("title")
    # <title>The Dormouse's story</title>
    
    https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-parents-find-parent
    

    4.2.3.4 修改文档树

    https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id40

    4.2.3.5 CSS选择器

    # 该模块提供了select方法来支持css,详见官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37
    
    
    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title">
        <b>The Dormouse's story</b>
        Once upon a time there were three little sisters; and their names were
        <a href="http://example.com/elsie" class="sister" id="link1">
            <span>Elsie</span>
        </a>
        <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
        <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
        <div class='panel-1'>
            <ul class='list' id='list-1'>
                <li class='element'>Foo</li>
                <li class='element'>Bar</li>
                <li class='element'>Jay</li>
            </ul>
            <ul class='list list-small' id='list-2'>
                <li class='element'><h1 class='yyyy'>Foo</h1></li>
                <li class='element xxx'>Bar</li>
                <li class='element'>Jay</li>
            </ul>
        </div>
        and they lived at the bottom of a well.
    </p>
    <p class="story">...</p>
    """
    
    
    
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html_doc,'lxml')
    
    
    #1、CSS选择器
    print(soup.p.select('.sister'))
    print(soup.select('.sister span'))
    
    print(soup.select('#link1'))
    print(soup.select('#link1 span'))
    
    print(soup.select('#list-2 .element.xxx'))
    
    print(soup.select('#list-2')[0].select('.element')) #可以一直select,但其实没必要,一条select就可以了
    
    # 2、获取属性
    print(soup.select('#list-2 h1')[0].attrs)
    
    # 3、获取内容
    print(soup.select('#list-2 h1')[0].get_text())
    

    4.2.3.6 总结

    # 总结:
    
    #1、推荐使用lxml解析库
    
    #2、三种选择器:
    	标签选择器,find与find_all,css选择器
        1、标签选择器筛选功能弱,但是速度快
        2、建议使用find,find_all查询匹配单个结果或者多个结果
        3、如果对css选择器非常熟悉建议使用select
        
    #3、获取属性attrs和获取文本值get_text()
    

    5 代理池搭建

    # 从github下载免费代理池开源代码(建议阅读源代码)
    git clone git@github.com:jhao104/proxy_pool.git
        
        
    # pycharm打开,修改配置文件(reids地址修改)
    # 启动爬虫:
    python3 proxyPool.py schedule
    # 启动服务:
    python3 proxyPool.py server
    
    
    # 随机获取代理
    requests.get("http://127.0.0.1:5010/get/").json()
    # 删除一个代理
    requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy))
    

    6 验证码破解之打码平台

    # 验证码破解的方法:
    	1 图像处理(困难)
        	pytesseract
            百度文字识别
            pillow
    	2 专业打码平台,破解验证码(收费)
    
        
    # 打码平台:
    # 申请超级鹰,注册
    # 登录,下载sdk(代码如下),填入用户名密码,软件id
    
    
    
    # !/usr/bin/env python
    # coding:utf-8
    import requests
    from hashlib import md5
    
    class Chaojiying_Client():
    
        def __init__(self, username, password, soft_id):
            self.username = username
            password =  password.encode('utf8')
            self.password = md5(password).hexdigest()
            self.soft_id = soft_id
            self.base_params = {
                'user': self.username,
                'pass2': self.password,
                'softid': self.soft_id,
            }
            self.headers = {
                'Connection': 'Keep-Alive',
                'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
            }
    
        def PostPic(self, im, codetype):
            """
            im: 图片字节
            codetype: 题目类型 参考 http://www.chaojiying.com/price.html
            """
            params = {
                'codetype': codetype,
            }
            params.update(self.base_params)
            files = {'userfile': ('ccc.jpg', im)}
            r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
            return r.json()
    
        def ReportError(self, im_id):
            """
            im_id:报错题目的图片ID
            """
            params = {
                'id': im_id,
            }
            params.update(self.base_params)
            r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
            return r.json()
    
    
    if __name__ == '__main__':
        chaojiying = Chaojiying_Client('306334678', 'lqz12345', '903641')	
        #用户中心>>软件ID 生成一个替换 96001
        im = open('a.jpg', 'rb').read()
        #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        print(chaojiying.PostPic(im, 1902))
        #1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    
    

    7 爬拉勾网职位信息

    爬取拉勾网需要先获取第一个页面的cookie,

    携带该cookie才能跳转到下一个页面

    # https://www.lagou.com/jobs/positionAjax.json?city=%E4%B8%8A%E6%B5%B7&needAddtionalResult=false
    
    
    import requests
    
    
    payload = {
        'first': 'true',
        'pn': '1',
        'kd': 'python',
    }
    
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
        'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
        'Accept': 'application/json, text/javascript, */*; q=0.01'
    }
    
    # 实际要爬取的url
    url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
    
    # 原始的url
    urls ='https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
    # 建立session,可以自动携带cookie
    s = requests.Session()
    # 获取搜索页的cookies
    s.get(urls, headers=header, timeout=3)# 可从s.cookies中获取cookie
    # 获取此次文本
    response = s.post(url, data=payload, headers=header, timeout=5).text
    print(response)
    
    

    8 爬《三国演义》

    # https://www.shicimingju.com/book/sanguoyanyi.html
    
    def write(name, title, content):
        with open(f'{name}.txt', 'a', encoding='utf-8') as f:
            f.write(title)
            f.write('
    ')
            f.write(content)
            f.write('
    
    ')
    
    
    for num in range(1,500):
        res = requests.get(f'https://www.shicimingju.com/book/sanguoyanyi/{num}.html', )
        soup = BeautifulSoup(res.text, 'lxml')
        try:
            name = soup.select('#nav-top a:last-child')[0].text
            title = soup.h1.text
            print(title)
            content = soup.select(".chapter_content")[0].text
            # print(content)
            write(name, title, content)
            print(f'第{num}章下完了')
            # break
        except Exception as e:
            # print(e)
            print(f'{title}下完了')
            break
    

    9 爬肯德基门店

    # http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword
    import requests
    
    
    def write(store_list):
        with open('kfc_sh.txt', 'a', encoding='utf-8') as f:
            # id = 1
            for store in store_list:
                f.write(
                    f"id:{store['rownum']},storeName:{store['storeName']},addressDetail:{store['addressDetail']},pro:{store['pro']}")
                f.write('
    
    ')
                print(f"已下载{store['rownum']}/{num}")
    
    
    for page in range(1, 20000):
        data = {'cname': '上海', 'pid': '', 'pageIndex': page, 'pageSize': 10}
        res = requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname', data=data)
        # print(res.text)
        store_list = res.json().get('Table1')
        if not store_list:
            break
        # print(store_list)
        num = res.json().get('Table')[0].get('rowcount')
        write(store_list)
    
    

    10 爬糗事百科段子

    #https://www.qiushibaike.com/text/page/2/
    
    import requests
    from bs4 import BeautifulSoup
    
    
    ret=requests.get('https://www.qiushibaike.com/text/page/2/')
    # print(ret.text)
    
    soup=BeautifulSoup(ret.text,'html.parser')
    
    article_list=soup.find_all(class_='article')
    # print(article_list)
    for article in article_list:
        content=article.find(class_='content').text
        print(content)
        print('-------')
    

    11 selenium使用

    11.1 selenium简介

    selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题
    
    selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器
    
    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser=webdriver.Firefox()
    browser=webdriver.PhantomJS()
    browser=webdriver.Safari()
    browser=webdriver.Edge() 
    

    11.2 selenium安装

    11.2.1 常规浏览器

    #安装:selenium+chromedriver
    pip3 install selenium
    下载chromdriver.exe放到python安装路径的scripts目录中即可
    国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/
    最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads
    
            
            
    #验证安装
    C:UsersAdministrator>python3
    Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    from selenium import webdriver
    driver=webdriver.Chrome() #弹出浏览器
    driver.get('https://www.baidu.com')
    driver.page_source
    
    
    
    #注意:
    selenium3默认支持的webdriver是Firfox,而Firefox需要安装geckodriver
    下载链接:https://github.com/mozilla/geckodriver/releases
    

    11.2.2 无界面浏览器

    PhantomJS(不再更新)

    #安装:selenium+phantomjs
    pip3 install selenium
    下载phantomjs,解压后把phantomjs.exe所在的bin目录放到环境变量
    下载链接:http://phantomjs.org/download.html
    
        
    #验证安装
    C:UsersAdministrator>phantomjs
    phantomjs> console.log('egon gaga')
    egon gaga
    undefined
    phantomjs>
    
    
    
    C:UsersAdministrator>python3
    Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    from selenium import webdriver
    driver=webdriver.PhantomJS() #无界面浏览器
    driver.get('https://www.baidu.com')
    driver.page_source
    

    在 PhantomJS 年久失修, 后继无人的节骨眼

    Chrome 出来救场, 再次成为了反爬虫 Team 的噩梦

    自Google 发布 chrome 59 / 60 正式版 开始便支持Headless mode

    这意味着在无 GUI 环境下, PhantomJS 不再是唯一选择

    # selenium:3.12.0
    # webdriver:2.38
    # chrome.exe: 65.0.3325.181(正式版本) (32 位)
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument('window-size=1920x3000') # 指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面
    chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
    chrome_options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
    
    
    bro=webdriver.Chrome(chrome_options=chrome_options,executable_path='./chromedriver.exe')
    driver.get('https://www.baidu.com')
    print(bro.page_source)
    print('hao123' in driver.page_source)
    driver.close() #切记关闭浏览器,回收资源
    

    11.3 selenium基本使用

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By  # 按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
    
    browser = webdriver.Chrome(executable_path='./chromedriver.exe')	# 指定驱动
    # 打开一个Chrome浏览器
    try:
        browser.get('https://www.baidu.com')	# 输入url地址
    
        input_tag = browser.find_element_by_id('kw')	# 找到搜索框
        input_tag.send_keys('美女')  # 在搜索框中加入内容	python2中输入中文错误,字符串前加个u
        input_tag.send_keys(Keys.ENTER)  # 输入回车
    
        wait = WebDriverWait(browser, 10)
        wait.until(EC.presence_of_element_located((By.ID, 'content_left')))  # 等到id为content_left的元素加载完毕,最多等10秒
    
        print(browser.page_source)
        print(browser.current_url)
        print(browser.get_cookies())
    
    
    finally:
        browser.close()
    

    11.4 selenium的选择器

    # 官网链接:http://selenium-python.readthedocs.io/locating-elements.html
    
    
    #===============所有方法===================
    # 1、find_element_by_id		# 通过id查找标签
    # 2、find_element_by_link_text		# 通过链接文本查找链接标签
    # 3、find_element_by_partial_link_text		# 通过链接的部分文本查找链接标签
    # 4、find_element_by_tag_name		# 通过标签(span)查找标签
    # 5、find_element_by_class_name		# 通过标签class查找标签
    # 6、find_element_by_name		# 通过标签name属性查找标签
    # 7、find_element_by_css_selector		# 通过css选择器查找标签
    # 8、find_element_by_xpath		# 通过xpath查找标签
    
    # 强调:
    # 1、上述均可以改写成find_element(By.ID,'kw')的形式
    # 2、find_elements_by_xxx的形式是查找到多个元素,结果为列表
    
    
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys #键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
    import time
    
    driver=webdriver.Chrome(executable_path='./chromedriver.exe')
    driver.get('https://www.baidu.com')
    wait=WebDriverWait(driver,10)
    
    
    #===============示范用法===================
    # 1、find_element_by_id
    print(driver.find_element_by_id('kw'))
    # 查找id为kw的标签
    
    # 2、find_element_by_link_text
    login=driver.find_element_by_link_text('登录')
    login.click()
    # 查找链接文本为登录的链接标签,并点击它
    
    # 3、find_element_by_partial_link_text
    login=driver.find_elements_by_partial_link_text('录')[0]
    login.click()
    # 查找链接文本中有录的链接标签,并点击它
    
    # 4、find_element_by_tag_name
    print(driver.find_element_by_tag_name('a'))
    # 查找第一个a标签
    
    # 5、find_element_by_class_name
    button=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'tang-pass-footerBarULogin')))
    button.click()
    # 查找class为tang-pass-footerBarULogin的标签,并点击它
    
    
    # 6、find_element_by_name
    input_user=wait.until(EC.presence_of_element_located((By.NAME,'userName')))
    input_pwd=wait.until(EC.presence_of_element_located((By.NAME,'password')))
    commit=wait.until(EC.element_to_be_clickable((By.ID,'TANGRAM__PSP_10__submit')))
    # 查找name属性为userName和password的标签
    
    
    input_user.send_keys('18611453110')
    # 在input标签内输入18611453110
    input_pwd.send_keys('xxxxxx')
    # 在input标签内输入xxxxxx
    commit.click()
    # 点击登录
    
    # 7、find_element_by_css_selector
    driver.find_element_by_css_selector('#kw')
    # 查找id为kw的标签(css选择器)
    
    # 8、find_element_by_xpath
    driver.find_element_by_xpath('//a')
    # 查找所有a标签(xpath)
    

    11.5 模拟登录百度

    # 模拟登陆百度
    from selenium import webdriver
    import time
    
    bro = webdriver.Chrome(executable_path='./chromedriver.exe')
    
    bro.get('https://www.baidu.com/')
    time.sleep(0.01)
    input_k = bro.find_element_by_id('kw')
    input_k.send_keys('美女')  # 在框里写入美女
    time.sleep(2)
    sou = bro.find_element_by_id('su')  # 找到搜索按钮
    sou.click()  # 点击搜索按钮
    time.sleep(4)
    bro.close()
    
    from selenium import webdriver
    import time
    
    bro = webdriver.Chrome(executable_path='./chromedriver.exe')
    bro.implicitly_wait(5)  # 隐式等待:找一个控件,如果控件没有加载出来,等待5s中  等待所有,只需要写着一句,以后找所有控件都按这个操作来
    bro.get('https://www.baidu.com/')
    
    d_button = bro.find_element_by_link_text('登录')
    
    d_button.click()
    
    login_u = bro.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn')
    login_u.click()
    
    username = bro.find_element_by_id('TANGRAM__PSP_11__userName')
    username.send_keys('yxp654799481')
    password = bro.find_element_by_id('TANGRAM__PSP_11__password')
    password.send_keys('yxp997997')
    time.sleep(3)
    submit = bro.find_element_by_id('TANGRAM__PSP_11__submit')
    
    submit.click()
    time.sleep(10)
    
    print(bro.get_cookies())
    
    bro.close()
    

    11.6 xpath选择器使用

    # xpath: XPath 是一门在 XML 文档中查找信息的语言
    # / :从根节点选取。
    # // :不管位置,直接找
    # /@属性名		获取属性
    # /text()	获取文本内容
    # 可以从浏览器复制(copy xpath)
    
    doc = '''
    <html>
     <head>
      <base href='http://example.com/' />
      <title>Example website</title>
     </head>
     <body>
      <div id='images'>
       <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
       <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
       <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
       <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
       <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
       <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
      </div>
     </body>
    </html>
    '''
    from lxml import etree
    
    html = etree.HTML(doc)
    # html=etree.parse('search.html',etree.HTMLParser())
    # 1 所有节点
    a = html.xpath('//*')
    # 2 指定节点(结果为列表)
    a = html.xpath('//head')
    # 3 子节点,子孙节点
    a = html.xpath('//div/a')
    a = html.xpath('//body/a')  # 无数据
    a = html.xpath('//body//a')
    # 4 父节点
    a = html.xpath('//body//a[@href="image1.html"]/..')
    a = html.xpath('//body//a[1]/..')
    # 也可以这样
    a = html.xpath('//body//a[1]/parent::*')
    # 5 属性匹配
    a = html.xpath('//body//a[@href="image1.html"]')
    
    # 6 文本获取	/text() 取当前标签的文本
    a = html.xpath('//body//a[@href="image1.html"]/text()')
    
    # 7 属性获取  	@href 取当前标签的属性
    a = html.xpath('//body//a/@href')
    # 注意从1 开始取(不是从0)
    a = html.xpath('//body//a[1]/@href')
    # 8 属性多值匹配
    # a 标签有多个class类,直接匹配就不可以了,需要用contains
    a = html.xpath('//body//a[@class="li"]')
    a = html.xpath('//body//a[contains(@class,"li")]')
    a = html.xpath('//body//a[contains(@class,"li")]/text()')
    # 9 多属性匹配
    a = html.xpath('//body//a[contains(@class,"li") or @name="items"]')
    a = html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
    # a=html.xpath('//body//a[contains(@class,"li")]/text()')
    # 10 按序选择
    a = html.xpath('//a[2]/text()')
    a = html.xpath('//a[2]/@href')
    # 取最后一个
    a = html.xpath('//a[last()]/@href')
    # 位置小于3的
    a = html.xpath('//a[position()<3]/@href')
    # 倒数第二个
    a = html.xpath('//a[last()-2]/@href')
    # 11 节点轴选择
    # ancestor:祖先节点
    # 使用了* 获取所有祖先节点
    a = html.xpath('//a/ancestor::*')
    # 获取祖先节点中的div
    a = html.xpath('//a/ancestor::div')
    # attribute:属性值
    a = html.xpath('//a[1]/attribute::*')
    # child:直接子节点
    a = html.xpath('//a[1]/child::*')
    # descendant:所有子孙节点
    a = html.xpath('//a[6]/descendant::*')
    # following:当前节点之后所有节点
    a = html.xpath('//a[1]/following::*')
    a = html.xpath('//a[1]/following::*[1]/@href')
    # following-sibling:当前节点之后同级节点
    a = html.xpath('//a[1]/following-sibling::*')
    a = html.xpath('//a[1]/following-sibling::a')
    a = html.xpath('//a[1]/following-sibling::*[2]')
    a = html.xpath('//a[1]/following-sibling::*[2]/@href')
    
    print(a)
    
    
    # 官网链接:http://selenium-python.readthedocs.io/locating-elements.html
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By  # 按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
    import time
    
    driver = webdriver.PhantomJS()
    driver.get('https://doc.scrapy.org/en/latest/_static/selectors-sample1.html')
    # wait=WebDriverWait(driver,3)
    driver.implicitly_wait(3)  # 使用隐式等待
    
    try:
        # find_element_by_xpath
        # //与/
        # driver.find_element_by_xpath('//body/a')  # 开头的//代表从整篇文档中寻找,body之后的/代表body的儿子,这一行找不到就会报错了
    
        driver.find_element_by_xpath('//body//a')  # 开头的//代表从整篇文档中寻找,body之后的//代表body的子子孙孙
        driver.find_element_by_css_selector('body a')
    
    
        # 取第n个
        res1 = driver.find_elements_by_xpath('//body//a[1]')  # 取第一个a标签
        print(res1[0].text)
        
        # 按照属性查找,下述三者查找效果一样
        res1 = driver.find_element_by_xpath('//a[5]')
        res2 = driver.find_element_by_xpath('//a[@href="image5.html"]')
        res3 = driver.find_element_by_xpath('//a[contains(@href,"image5")]')  # 模糊查找
        print('==>', res1.text)
        print('==>', res2.text)
        print('==>', res3.text)
        
        # 其他
        res1 = driver.find_element_by_xpath('/html/body/div/a')
        print(res1.text)
        
        res2 = driver.find_element_by_xpath('//a[img/@src="image3_thumb.jpg"]')  
        # 找到子标签img的src属性为image3_thumb.jpg的a标签
        print(res2.tag_name, res2.text)
        
        res3 = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")  
        # 查看属性name为continue且属性type为button的input标签
        res4 = driver.find_element_by_xpath("//*[@name='continue'][@type='button']")  
        # 查看属性name为continue且属性type为button的所有标签
        
        time.sleep(5)
    
    
    finally:
        driver.close()
    

    11.7 selenium标签的属性

    .text
    .location
    .size
    .get_attribute('href')
    .get_cookies()
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys #键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
    
    browser=webdriver.Chrome()
    
    browser.get('https://www.amazon.cn/')
    
    wait=WebDriverWait(browser,10)
    wait.until(EC.presence_of_element_located((By.ID,'cc-lm-tcgShowImgContainer')))
    
    tag=browser.find_element(By.CSS_SELECTOR,'#cc-lm-tcgShowImgContainer img')
    
    #获取标签属性,
    print(tag.text)   # 获取文本内容
    print(tag.get_attribute('src'))	# 获取tag的src属性
    
    #获取标签ID,位置,名称,大小(了解)
    print(tag.id)
    print(tag.location)
    print(tag.tag_name)
    print(tag.size)
    
    browser.close()
    

    11.8 隐式等待和显式等待

    #1、selenium只是模拟浏览器的行为,而浏览器解析页面是需要时间的(执行css,js),一些元素可能需要过一段时间才能加载出来,为了保证能查找到元素,必须等待
    
    #2、等待的方式分两种:
    隐式等待:在browser.get('xxx')前就设置,针对所有元素有效
    显式等待:在browser.get('xxx')之后设置,只针对某个元素有效
    

    11.8.1 隐式等待

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By  # 按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
    
    browser = webdriver.Chrome()
    
    # 隐式等待:在查找所有元素时,如果尚未被加载,则等10秒
    browser.implicitly_wait(10)
    
    browser.get('https://www.baidu.com')
    
    input_tag = browser.find_element_by_id('kw')
    input_tag.send_keys('美女')
    input_tag.send_keys(Keys.ENTER)
    
    contents = browser.find_element_by_id('content_left')  # 没有等待环节而直接查找,找不到则会报错
    print(contents)
    
    browser.close()
    

    11.8.2 显式等待

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By  # 按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
    
    browser = webdriver.Chrome()
    browser.get('https://www.baidu.com')
    
    input_tag = browser.find_element_by_id('kw')
    input_tag.send_keys('美女')
    input_tag.send_keys(Keys.ENTER)
    
    # 显式等待:显式地等待某个元素被加载
    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_element_located((By.ID, 'content_left')))
    
    contents = browser.find_element(By.CSS_SELECTOR, '#content_left')
    print(contents)
    
    browser.close()
    

    11.9 元素交互操作

    11.9.1 简单交互

    input_tag.clear() 	# 清空输入框
    input_tag.send_keys('iphone7plus')	# 在输入框增加内容
    button.click()		# 点击该标签
    input_tag.send_keys(Keys.ENTER)		# 按下回车键
    browser.execute_script('alert("hello world")')	# 执行js代码
    
    
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    

    11.9.2 动作链

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By  # 按照什么方式查找,By.ID,By.CSS_SELECTOR
    from selenium.webdriver.common.keys import Keys  # 键盘按键操作
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
    import time
    
    driver = webdriver.Chrome()
    driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    wait = WebDriverWait(driver, 3)
    # driver.implicitly_wait(3)  # 使用隐式等待
    
    try:
        driver.switch_to.frame('iframeResult')  ##切换到iframeResult
        sourse = driver.find_element_by_id('draggable')
        target = driver.find_element_by_id('droppable')
    
        # 方式一:基于同一个动作链串行执行
        # actions=ActionChains(driver) #拿到动作链对象
        # actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行
        # actions.perform()
    
        # 方式二:不同的动作链,每次移动的位移都不同
    
        ActionChains(driver).click_and_hold(sourse).perform()
        distance = target.location['x'] - sourse.location['x']
    
        track = 0
        while track < distance:
            ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
            track += 2
    
        ActionChains(driver).release().perform()
    
        time.sleep(10)
    
    
    finally:
        driver.close()
    

    11.9.3 执行js代码

    from selenium import webdriver
    
    browser = webdriver.Chrome()
    try:
        browser.get('https://www.baidu.com')
        browser.execute_script('alert("hello world")')  # 打印警告
    finally:
        browser.close()
    

    11.9.4 切换frame

    # frame相当于一个单独的网页,在父frame里是无法直接查看到子frame的元素的,必须switch_to_frame切到该frame下,才能进一步查找
    
    from selenium import webdriver
    
    browser = webdriver.Chrome()
    
    try:
        browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    
        browser.switch_to.frame('iframeResult')  # 切换到id为iframeResult的frame
    
        tag1 = browser.find_element_by_id('droppable')
        print(tag1)
    
        # tag2=browser.find_element_by_id('textareaCode') 
        # 报错,在子frame里无法查看到父frame的元素
        browser.switch_to.parent_frame()  # 切回父frame,就可以查找到了
        tag2 = browser.find_element_by_id('textareaCode')
        print(tag2)
    
    finally:
        browser.close()
    

    11.10 其他

    11.10.1 模拟浏览器的前进后退

    import time
    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser.get('https://www.baidu.com')
    browser.get('https://www.taobao.com')
    browser.get('http://www.sina.com.cn/')
    
    browser.back()
    time.sleep(10)
    browser.forward()
    browser.close()
    
    # 如何把屏幕拉倒最后(js控制)
    # bro.execute_script('window.scrollTo(0,document.body.offsetHeight)')
    

    11.10.2 获取cookie

    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser.get('https://www.zhihu.com/explore')
    print(browser.get_cookies())
    browser.add_cookie({'k1':'xxx','k2':'yyy'})
    print(browser.get_cookies())
    
    # browser.delete_all_cookies()
    

    11.10.3 选项卡管理

    # 选项卡管理:切换选项卡,有js的方式windows.open,有windows快捷键:ctrl+t等,最通用的就是js的方式
    import time
    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser.get('https://www.baidu.com')
    browser.execute_script('window.open()')
    
    print(browser.window_handles) #获取所有的选项卡
    browser.switch_to_window(browser.window_handles[1])
    browser.get('https://www.taobao.com')
    time.sleep(10)
    browser.switch_to_window(browser.window_handles[0])
    browser.get('https://www.sina.com.cn')
    browser.close()
    

    11.10.4 异常处理

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException
    
    try:
        browser=webdriver.Chrome()
        browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
        browser.switch_to.frame('iframssseResult')
    
    except TimeoutException as e:
        print(e)
    except NoSuchFrameException as e:
        print(e)
    finally:
        browser.close()
    

    12 爬取京东商品信息

    from selenium import webdriver
    import time
    # 模拟键盘输入
    from selenium.webdriver.common.keys import Keys
    bro=webdriver.Chrome(executable_path='./chromedriver.exe')
    # 设置隐士等待
    bro.implicitly_wait(10)
    
    def get_goods_info(bro):
        # li_list=bro.find_element_by_class_name('gl-warp').find_elements_by_tag_name('li')
        # goods=bro.find_elements_by_class_name('gl-item')
        goods = bro.find_elements_by_css_selector('.gl-item')
        # print(len(goods))
        for good in goods:
            try:
                price = good.find_element_by_css_selector('.p-price i').text
                name = good.find_element_by_css_selector('.p-name em').text
                url = good.find_element_by_css_selector('.p-img a').get_attribute('href')
                commits = good.find_element_by_css_selector('.p-commit strong>a').text
                photo_url = good.find_element_by_css_selector('.p-img img').get_attribute('src')
    
                print('''
                商品名字:%s
                商品价格:%s
                商品地址:%s
                商品评论数:%s
                商品图片地址:%s
        
                ''' % (name, price, url, commits, photo_url))
            except Exception as e:
                continue
    
        next_button = bro.find_element_by_partial_link_text('下一页')
        time.sleep(1)
        next_button.click()
    
        get_goods_info(bro)
    
    try:
        bro.get('https://www.jd.com/')
    
        input_k=bro.find_element_by_id('key')
    
        input_k.send_keys('奶牛')
        # 模拟键盘的回车键
        input_k.send_keys(Keys.ENTER)
        get_goods_info(bro)
    
    
    except Exception as e:
        print(e)
    
    finally:
        bro.close()
    
    

    13 自动登录12306

    from selenium import webdriver
    import time
    # pip install pillow
    from PIL import Image
    # 引入超级鹰
    from chaojiying import Chaojiying_Client
    
    from selenium.webdriver import ActionChains
    
    bro = webdriver.Chrome(executable_path='./chromedriver.exe')
    bro.implicitly_wait(10)
    try:
        bro.get('https://kyfw.12306.cn/otn/resources/login.html')
        bro.maximize_window()  # 窗口最大化,全屏
        button_z = bro.find_element_by_css_selector('.login-hd-account a')
        button_z.click()
        time.sleep(2)
        # 截取整个屏幕
        bro.save_screenshot('./main.png')
        # 验证码的位置和大小
        img_t = bro.find_element_by_id('J-loginImg')
        print(img_t.size)
        print(img_t.location)
    
        size = img_t.size
        location = img_t.location
    
        img_tu = (
        int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
        # # 抠出验证码
        # #打开
        img = Image.open('./main.png')
        # 抠图
        fram = img.crop(img_tu)
        # 截出来的小图
        fram.save('code.png')
    
        # 调用超级鹰破解
        chaojiying = Chaojiying_Client('306334678', 'lqz12345', '903641')  # 用户中心>>软件ID 生成一个替换 96001
        im = open('code.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        # print(chaojiying.PostPic(im, 9004))
    
        ## 返回结果如果有多个 260,133|123,233,处理这种格式[[260,133],[123,233]]
        res = chaojiying.PostPic(im, 9004)
        print(res)
        result = res['pic_str']
    
        all_list = []
        if '|' in result:
            list_1 = result.split('|')
            count_1 = len(list_1)
            for i in range(count_1):
                xy_list = []
                x = int(list_1[i].split(',')[0])
                y = int(list_1[i].split(',')[1])
                xy_list.append(x)
                xy_list.append(y)
                all_list.append(xy_list)
        else:
            x = int(result.split(',')[0])
            y = int(result.split(',')[1])
            xy_list = []
            xy_list.append(x)
            xy_list.append(y)
            all_list.append(xy_list)
        print(all_list)
        # 用动作链,点击图片
        # [[260,133],[123,233]]
        for a in all_list:
            x = a[0]
            y = a[1]
            ActionChains(bro).move_to_element_with_offset(img_t, x, y).click().perform()
            time.sleep(1)
    
        username = bro.find_element_by_id('J-userName')
        username.send_keys('306334678')
        password = bro.find_element_by_id('J-password')
        password.send_keys('lqz12345')
        time.sleep(3)
        submit_login = bro.find_element_by_id('J-login')
        submit_login.click()
        time.sleep(3)
    
        print(bro.get_cookies())
        time.sleep(10)
        bro.get('https://www.12306.cn/index/')
        time.sleep(5)
    
    except Exception as e:
        print(e)
    finally:
        bro.close()
    
    

    14 cookie池

    # 如何搭建cookie池
    # selenium写一套(一堆小号),跑起脚本,自动登录,手动参与
    # 拿到cookie,放到redis中
    # django搭建一个服务:127.0.0.0/get,随机返回一个cookie
    # request发送请求爬数据(selenium拿到的cookie),cookie失效
    

    15 抓包工具介绍

    # 1 浏览器调试模式
    # 2 fiddler,charles(自己研究一下)
    

    16 scrapy

    16.1 scrapy简介

    # scrapy是通用的网络爬虫框架,爬虫界的django
    
    # scrapy的五大组件
        -引擎(EGINE):引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件
        -调度器(SCHEDULER):用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址
        -下载器(DOWLOADER):用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的
        -爬虫(SPIDERS):开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求request
        -项目管道(ITEM PIPLINES):在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作
            
           
        
    # scrapy的两大中间件
        -爬虫中间件:位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入和输出(用的很少)
        -下载中间件:位于引擎和下载器之间,可以加代理,加请求头,集成selenium        
    
    

    scrapy执行流程,架构图

    scrapy架构图

    16.2 scrapy安装

    # 1 pip3 install scrapy(mac,linux可以正常执行)
    
    # 2 windows执行该命令,可能能直接成功,少部分人成功不了
    
    # 报错解决方案:
    	1、pip3 install wheel 
        # 安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pythonlibs
        2、pip3 install lxml
        3、pip3 install pyopenssl
        4、pip3 install pywin32
        5、下载twisted的wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
        6、执行pip3 install 下载目录Twisted-17.9.0-cp36-cp36m-win_amd64.whl
        7、pip3 install scrapy
        
        
    # 3 验证安装成功:命令行输入scrapy,有反应即安装完成
    	Python36Scriptsscrapy.exe  该文件用于创建项目
    

    16.3 scrapy创建项目,创建爬虫,运行爬虫

    # 1 创建项目
    	-scrapy startproject 项目名
    	-scrapy startproject firstscrapy
    # 2 创建爬虫
    	-scrapy genspider 爬虫名 爬虫地址
        -scrapy genspider chouti dig.chouti.com
        -执行后会在spider文件夹下创建一个py文件,名字为chouti
    # 3 运行爬虫
    	-scrapy crawl chouti   # 输出日志
        -scrapy crawl chouti --nolog  # 不输出日志
    # 4 支持右键执行爬虫
    	-在项目路径下新建一个main.py
        from scrapy.cmdline import execute
    	execute(['scrapy','crawl','chouti','--nolog'])
    

    16.4 目录介绍

    firstscrapy  # 项目名字
        firstscrapy # 包
            -spiders # 包含所有的爬虫文件
                -baidu.py # 一个个的爬虫
                -chouti.py
            -middlewares.py # 中间件(爬虫中间件,下载中间件)
            -pipelines.py   # 持久化相关(操作items.py中类的对象)
            -main.py        # 用于执行爬虫(自己创建)
            -items.py       # 写item类
            -settings.py    # 配置文件
        scrapy.cfg          # 上线相关
    

    16.5 settings部分参数

    1 默认情况,scrapy会遵循爬虫协议,修改配置文件参数,不遵循协议,强行爬取。
    	ROBOTSTXT_OBEY = False
        
    2 设置全局USER_AGENT
    	USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
    
    3 设置日志等级LOG_LEVEL
    	LOG_LEVEL = 'ERROR'
    

    16.6 scrapy的数据解析

    # xpath选择:
        -response.xpath('//a[contains(@class,"link-title")]/text()').extract()  # 取文本
        -response.xpath('//a[contains(@class,"link-title")]/@href').extract()  # 取属性
    # css选择:
        -response.css('.link-title::text').extract()  # 取文本
        -response.css('.link-title::attr(href)').extract_first()  # 取属性
    
    response.selector.css()
    response.selector.xpath()
    可简写为
    response.css()
    response.xpath()
    
    #1 //与/
    response.xpath('//body/a/')#
    response.css('div a::text')
    
    response.xpath('//body/a') #开头的//代表从整篇文档中寻找,body之后的/代表body的儿子
    []
    response.xpath('//body//a') #开头的//代表从整篇文档中寻找,body之后的//代表body的子子孙孙
    [<Selector xpath='//body//a' data='<a href="image1.html">Name: My image 1 <'>, <Selector xpath='//body//a' data='<a href="image2.html">Name: My image 2 <'>, <Selector xpath='//body//a' data='<a href="
    image3.html">Name: My image 3 <'>, <Selector xpath='//body//a' data='<a href="image4.html">Name: My image 4 <'>, <Selector xpath='//body//a' data='<a href="image5.html">Name: My image 5 <'>]
    
    #2 text
    response.xpath('//body//a/text()')
    response.css('body a::text')
    
    #3、extract与extract_first:从selector对象中解出内容
    response.xpath('//div/a/text()').extract()
    ['Name: My image 1 ', 'Name: My image 2 ', 'Name: My image 3 ', 'Name: My image 4 ', 'Name: My image 5 ']
    response.css('div a::text').extract()
    ['Name: My image 1 ', 'Name: My image 2 ', 'Name: My image 3 ', 'Name: My image 4 ', 'Name: My image 5 ']
    
    response.xpath('//div/a/text()').extract_first()
    'Name: My image 1 '
    response.css('div a::text').extract_first()
    'Name: My image 1 '
    
    #4、属性:xpath的属性加前缀@
    response.xpath('//div/a/@href').extract_first()
    'image1.html'
    response.css('div a::attr(href)').extract_first()
    'image1.html'
    
    #4、嵌套查找
    response.xpath('//div').css('a').xpath('@href').extract_first()
    'image1.html'
    
    #5、设置默认值
    response.xpath('//div[@id="xxx"]').extract_first(default="not found")
    'not found'
    
    #4、按照属性查找
    response.xpath('//div[@id="images"]/a[@href="image3.html"]/text()').extract()
    response.css('#images a[@href="image3.html"]/text()').extract()
    
    #5、按照属性模糊查找
    response.xpath('//a[contains(@href,"image")]/@href').extract()
    response.css('a[href*="image"]::attr(href)').extract()
    
    response.xpath('//a[contains(@href,"image")]/img/@src').extract()
    response.css('a[href*="imag"] img::attr(src)').extract()
    
    response.xpath('//*[@href="image1.html"]')
    response.css('*[href="image1.html"]')
    
    #6、正则表达式
    response.xpath('//a/text()').re(r'Name: (.*)')
    response.xpath('//a/text()').re_first(r'Name: (.*)')
    
    #7、xpath相对路径
    res=response.xpath('//a[contains(@href,"3")]')[0]
    res.xpath('img')
    [<Selector xpath='img' data='<img src="image3_thumb.jpg">'>]
    res.xpath('./img')
    [<Selector xpath='./img' data='<img src="image3_thumb.jpg">'>]
    res.xpath('.//img')
    [<Selector xpath='.//img' data='<img src="image3_thumb.jpg">'>]
    res.xpath('//img') #这就是从头开始扫描
    [<Selector xpath='//img' data='<img src="image1_thumb.jpg">'>, <Selector xpath='//img' data='<img src="image2_thumb.jpg">'>, <Selector xpath='//img' data='<img src="image3_thumb.jpg">'>, <Selector xpa
    th='//img' data='<img src="image4_thumb.jpg">'>, <Selector xpath='//img' data='<img src="image5_thumb.jpg">'>]
    
    #8、带变量的xpath
    response.xpath('//div[@id=$xxx]/a/text()',xxx='images').extract_first()
    'Name: My image 1 '
    response.xpath('//div[count(a)=$yyy]/@id',yyy=5).extract_first() #求有5个a标签的div的id
    'images'
    

    16.7 scrapy的持久化存储

    # 1 方案一:
    	parser函数必须返回列表套字典的形式(了解)
    	执行命令:scrapy crawl 爬虫名 -o 文件名
        
    # 2 方案二:通过pipline,item存储(mysql,redis,文件)
    	-在Items.py中写一个类
        -在spider中导入,实例化,把数据放进去
        	    item['title']=title
                item['url']=url
                item['photo_url']=photo_url
                yield item
                
        -在setting中配置(数字越小,级别越高)
        	ITEM_PIPELINES = {
       		'firstscrapy.pipelines.ChoutiFilePipeline': 300,
    		}
        -在pipelines.py中写ChoutiFilePipeline
        	-open_spider(开始的时候)
            -close_spider(结束的时候)
            -process_item(在这持久化)
    
    # 方案2
    # items.py
    import scrapy
    
    class ChoutiItem(scrapy.Item):
        title = scrapy.Field()
        url = scrapy.Field()
        photo_url = scrapy.Field()
        
        
        
    # pipelines.py
    class ChoutiFilePipeline(object):
        def open_spider(self,spider):
            print('打开文件')
            self.file = open('chouti.txt','w',encoding='utf-8')
        def process_item(self, item, spider):
            print('111')
            self.file.write(item['title']+'
    ')
            self.file.write(item['url']+'
    ')
            self.file.write(item['photo_url']+'
    ')
            return item
        def close_spider(self,spider):
            print('关闭文件')
            self.file.close()
            
            
    # settings.py
    ITEM_PIPELINES = {
       		'firstscrapy.pipelines.ChoutiFilePipeline': 300,
    		}
    

    16.8 scrapy命令

    #1 查看帮助
        scrapy -h
        scrapy <command> -h
    
    #2 有两种命令:其中Project-only必须切到项目文件夹下才能执行,而Global的命令则不需要
        Global commands:
            startproject #创建项目
            genspider    #创建爬虫程序
            settings     #如果是在项目目录下,则得到的是该项目的配置
            runspider    #运行一个独立的python文件,不必创建项目
            shell        #scrapy shell url地址  在交互式调试,如选择器规则正确与否
            fetch        #独立于程单纯地爬取一个页面,可以拿到请求头
            view         #下载完毕后直接弹出浏览器,以此可以分辨出哪些数据是ajax请求
            version      #scrapy version 查看scrapy的版本,scrapy version -v查看scrapy依赖库的版本
        Project-only commands:
            crawl        #运行爬虫,必须创建项目才行,确保配置文件中ROBOTSTXT_OBEY = False
            check        #检测项目中有无语法错误
            list         #列出项目中所包含的爬虫名
            edit         #编辑器,一般不用
            parse        #scrapy parse url地址 --callback 回调函数  #以此可以验证我们的回调函数是否正确
            bench        #scrapy bentch压力测试
    
    #3 官网链接
        https://docs.scrapy.org/en/latest/topics/commands.html
    

    17 爬取抽屉新闻

    # chouti.py
    import scrapy
    from scrapy.http.request import Request
    from bs4 import BeautifulSoup
    from firstscrapy.items import ChoutiItem
    
    
    class ChoutiSpider(scrapy.Spider):
        name = 'chouti'
        allowed_domains = ['dig.chouti.com']
        start_urls = ['http://dig.chouti.com/']
        
        def parse(self, response):
            div_list=response.xpath('//div[contains(@class,"link-item")]')
            for div in div_list:
                item = ChoutiItem()
                title=div.css('.link-title::text').extract_first()
                url=div.css('.link-title::attr(href)').extract_first()
                photo_url=div.css('.image-scale::attr(src)').extract_first()
                if not photo_url:
                    photo_url=''
                item['title']=title
                item['url']=url
                item['photo_url']=photo_url
                yield item
                # 注意要用yield
    
    
    
    
    # items.py
    import scrapy
    class ChoutiItem(scrapy.Item):
        title = scrapy.Field()
        url = scrapy.Field()
        photo_url = scrapy.Field()
    
        
        
    # pipelines.py
    import pymysql
    
    class ChoutiFilePipeline(object):
        def open_spider(self,spider):
            print('打开文件')
            self.file=open('chouti.txt','w',encoding='utf-8')
            
        def process_item(self, item, spider):
            self.file.write(item['title']+'
    ')
            self.file.write(item['url']+'
    ')
            self.file.write(item['photo_url']+'
    ')
            return item
        
        def close_spider(self,spider):
            print('关闭文件')
            self.file.close()
    
    
    class ChoutiMysqlPipeline(object):
        
        def open_spider(self,spider):
            self.conn=pymysql.connect( host='127.0.0.1', user='root', password="123",
                     database='chouti', port=3306)
            
        def close_spider(self,spider):
            self.conn.close()
            
        def process_item(self, item, spider):
            cursor=self.conn.cursor()
            sql='insert into article (title,url,photo_url)values(%s,%s,%s) '
            cursor.execute(sql,[item['title'],item['url'],item['photo_url']])
            self.conn.commit()
            return item
    

    18 自动点赞

    from selenium import webdriver
    import time
    
    bro = webdriver.Chrome(executable_path=r'chromedriver.exe')
    bro.implicitly_wait(5)
    bro.get('https://dig.chouti.com/')
    bro.maximize_window()
    login_show = bro.find_element_by_id('login_btn')
    login_show.click()
    username = bro.find_element_by_name('phone')
    username.send_keys('13212106712')
    password = bro.find_element_by_name('password')
    password.send_keys('wwh123')
    login_button = bro.find_element_by_css_selector('button.login-btn')
    login_button.click()
    time.sleep(10)
    # 可能有验证码,手动操作一下
    
    
    with open('cookie.txt','w',encoding='utf-8') as f:
        f.write(str(bro.get_cookies()))
        # 这个cookie不是一个字典,不能直接给requests使用,需要转一下
    bro.close()
    
    
    
    # ===================================================================================
    import requests
    from bs4 import BeautifulSoup
    
    
    # 这个cookie不是一个字典,不能直接给requests使用,需要转一下
    with open('cookie.txt', 'rb') as f:
        cookie = f.read()
    cookies = eval(cookie)
    cookie = {}
    for i in cookies:
        cookie[i['name']] = i['value']
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
        'referer': 'https://dig.chouti.com/',
    
    }
    res = requests.get('https://dig.chouti.com/', cookies=cookie, headers=headers)
    soup = BeautifulSoup(res.text,'lxml')
    divs = soup.select('.link-con .link-item')
    ll = []
    for div in divs:
        id = div.get('data-id')
        ll.append(id)
    
    print(ll)
    
    
    res = requests.get('https://dig.chouti.com/top/24hr?_=159671243913', cookies=cookie, headers=headers, )
    # print(res.json())
    for item in res.json()['data']:
        ll.append(item['id'])
    
    
    
    res = requests.get('https://dig.chouti.com/top/72hr?_=1596712861433', cookies=cookie, headers=headers, )
    for item in res.json()['data']:
        ll.append(item['id'])
    
    res = requests.get('https://dig.chouti.com/top/168hr?_=1596712861434', cookies=cookie, headers=headers, )
    for item in res.json()['data']:
        ll.append(item['id'])
    
    print(ll)
    # https://dig.chouti.com/link/cancel/vote
    # linkId: 29832226
    headers['content-length'] = '15'
    for id in ll:
        res = requests.post('https://dig.chouti.com/link/vote', cookies=cookie, headers=headers, data={'linkId': id})
        print(res.text)
    
    

    19 爬取cnblogs全站

    # cnblog.py
    import scrapy
    from scrapy.http import Request
    from ..items import CNBlogItem
    
    
    # 爬取cnblogs文章,把标题连接地址和文章内容保存到mysql,连续爬取n页
    
    
    class CnblogSpider(scrapy.Spider):
        name = 'cnblog'
        allowed_domains = ['www.cnblogs.com']
        start_urls = ['http://www.cnblogs.com/']
    
        def parse(self, response):
            articles = response.css('#post_list article')
            for article in articles:
                item = CNBlogItem()
                title = article.css('.post-item-title::text').extract_first()
                url = article.css('.post-item-title::attr(href)').extract_first()
                print(title)
                item['title'] = title
                item['url'] = url
                request = Request(url, callback=self.parse_content)
                # 需要先爬取文章内容,为了将文章存入item,需要将item传递
                request.meta['item'] = item
                # 爬完后回调到parse_content继续执行代码
                yield request
            # 当页所有文章爬完后,获取下一页的url,继续请求,回调parse继续执行代码
            next_page = response.css('.pager a:last-child::attr(href)').extract_first()
            next_url = 'http://www.cnblogs.com' + next_page
            yield Request(next_url, callback=self.parse)
    
        def parse_content(self, response):
            item = response.meta.get('item')
            article = response.css('#topics').extract_first()
            item['content'] = article
            # print(item)
            return item
    
            pass
    # ====================================================================================
    class CNBlogItem(scrapy.Item):
        title = scrapy.Field()
        url = scrapy.Field()
        content = scrapy.Field()
    

    20 scrapy的请求传参

    # 把要传递的数据放到meta中
    yield Request(url,meta={'item':item})
    
    # 在response对象中取出来
    item=response.meta.get('item')
    

    21 提升scrapy爬取数据效率

    - 在配置文件中进行相关的配置即可:(默认还有一套setting)
    #1 增加并发:
    默认scrapy开启的并发线程为32个,可以适当进行增加。在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100。
    
    #2 降低日志级别:
    在运行scrapy时,会有大量日志信息的输出,为了减少CPU的使用率。可以设置log输出信息为INFO或者ERROR即可。在配置文件中编写:LOG_LEVEL = ‘INFO’
    
    # 3 禁止cookie:
    如果不是真的需要cookie,则在scrapy爬取数据时可以禁止cookie从而减少CPU的使用率,提升爬取效率。在配置文件中编写:COOKIES_ENABLED = False
    
    # 4禁止重试:
    对失败的HTTP进行重新请求(重试)会减慢爬取速度,因此可以禁止重试。在配置文件中编写:RETRY_ENABLED = False
    
    # 5 减少下载超时:
    如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速被放弃,从而提升效率。在配置文件中进行编写:DOWNLOAD_TIMEOUT = 10 超时时间为10s
    

    22 scrapy的中间件

    # 爬虫中间件,下载中间件都写在middlewares.py
    # 记得配置:配置文件
    
    
    # 下载中间件
    - process_request:返回不同的对象,后续处理不同(加代理...)
        # 1 更换请求头
        print(type(request.headers))	# Headers对象,继承了dict
        # from scrapy.http.headers import Headers
        request.headers['User-Agent']='xxx'
    
        # 2 加cookie ---cookie池
        # 假设已经搭建好cookie池
        request.cookies={'username':'xxxx'}
    
        # 3 加代理	-----代理池
        print(request.meta)
        request.meta['download_timeout'] = 20
        request.meta["proxy"] = 'http://27.188.62.3:8060'
            
            
            
    - process_response:返回不同的对象,后续处理不同
    
    
    - process_exception
    
    	def process_exception(self, request, exception, spider):
            print('xxxx')
            # request.url='https://www.baidu.com'
            # 不允许直接修改request的url,创建新的request对象
            from scrapy import Request
            request=Request(url='https://www.baidu.com',callback=spider.parser)
            return request
    
    
    

    23 在scrapy中的使用selenium

    # 注意:使用同一个浏览器
    
    # 1 在爬虫中初始化webdriver对象
    from selenium import webdriver
    
    class CnblogSpider(scrapy.Spider):
        name = 'cnblog'
        ...
        bro = webdriver.Chrome(executable_path='chromedriver.exe')
        
        
    # 2 在中间件中使用(process_request)
    spider.bro.get('https://dig.chouti.com/')
    response = HtmlResponse(url='https://dig.chouti.com/',
                            body=spider.bro.page_source.encode('utf-8'), 
                            request=request)
    return response
    
    
    # 3 在爬虫中关闭
    def close(self, reason):
        print("我结束了")
        self.bro.close()
    

    24 去重规则源码分析

    # scrapy模块的默认去重配置为:
    DUPEFILTER_CLASS = 'scrapy.dupefilters.RFPDupeFilter'
    
    
    # scrapy/dupefilters.py
    class RFPDupeFilter(BaseDupeFilter):
        """Request Fingerprint duplicates filter"""
    
        def __init__(self, path=None, debug=False):
            self.fingerprints = set()		# 使用集合去重
            
            
        def request_seen(self, request):
            fp = self.request_fingerprint(request)
            # fp是指纹,即使 查询参数 顺序不同也能识别
            # http://www.example.com/query?id=111&cat=222
            # http://www.example.com/query?cat=222&id=111
            # 内部使用sha1加密处理(hash)添加了请求方式,请求体等
            if fp in self.fingerprints:		# 如果已存在就返回True
                return True
            self.fingerprints.add(fp)	# 不存在则添加入集合
            if self.file:
                self.file.write(fp + '
    ')
    

    25 分布式爬虫(scrapy-redis)

    # 1 pip3 install scrapy-redis
    # 2 爬虫原来继承Spider,现在需要继承RedisSpider
    # 3 不能写start_urls = ['https:/www.cnblogs.com/']
    # 4 需要写redis_key = 'myspider:start_urls'
    # 5 setting中配置:
        # redis的连接
        REDIS_HOST = 'localhost'                            # 主机名
        REDIS_PORT = 6379                                   # 端口
            # 使用scrapy-redis的去重
        DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
        # 使用scrapy-redis的Scheduler
        # 分布式爬虫的配置
        SCHEDULER = "scrapy_redis.scheduler.Scheduler"
        # 持久化的可以配置,也可以不配置
        ITEM_PIPELINES = {
           'scrapy_redis.pipelines.RedisPipeline': 299
        }
    
    
    # 6 去redis中以myspider:start_urls为key,插入一个起始地址
    	lpush myspider:start_urls https://www.cnblogs.com/
    

    26 破解知乎登陆(js逆向和解密)

    client_id=c3cef7c66a1843f8b3a9e6a1e3160e20&
    grant_type=password&
    timestamp=1596702006088&
    source=com.zhihu.web&
    signature=eac4a6c461f9edf86ef33ef950c7b6aa426dbb39&
    username=%2B86liuqingzheng&
    password=1111111&
    captcha=&
    lang=en&
    utm_source=&
    ref_source=other_https%3A%2F%2Fwww.zhihu.com%2Fsignin%3Fnext%3D%252F"
    
    
    # 破解知乎登陆
    
    import requests    #请求解析库
    
    import base64							  #base64解密加密库
    from PIL import Image	  			      #图片处理库
    import hmac								  #加密库
    from hashlib import sha1				  #加密库
    import time
    from urllib.parse import urlencode		  #url编码库
    import execjs							  #python调用node.js
    from http import cookiejar as cookielib
    class Spider():
        def __init__(self):
            self.session = requests.session()
            self.session.cookies = cookielib.LWPCookieJar()    #使cookie可以调用save和load方法
            self.login_page_url = 'https://www.zhihu.com/signin?next=%2F'
            self.login_api = 'https://www.zhihu.com/api/v3/oauth/sign_in'
            self.captcha_api = 'https://www.zhihu.com/api/v3/oauth/captcha?lang=en'
            self.headers = {
                'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER',
            }
    
            self.captcha =''         #存验证码
            self.signature = ''	   #存签名
    
        # 首次请求获取cookie
        def get_base_cookie(self):
            self.session.get(url=self.login_page_url, headers=self.headers)
    
        def deal_captcha(self):
            r = self.session.get(url=self.captcha_api, headers=self.headers)
            r = r.json()
            if r.get('show_captcha'):
                while True:
                    r = self.session.put(url=self.captcha_api, headers=self.headers)
                    img_base64 = r.json().get('img_base64')
                    with open('captcha.png', 'wb') as f:
                        f.write(base64.b64decode(img_base64))
                    captcha_img = Image.open('captcha.png')
                    captcha_img.show()
                    self.captcha = input('输入验证码:')
                    r = self.session.post(url=self.captcha_api, data={'input_text': self.captcha},
                                          headers=self.headers)
                    if r.json().get('success'):
                        break
    
        def get_signature(self):
            # 生成加密签名
            a = hmac.new(b'd1b964811afb40118a12068ff74a12f4', digestmod=sha1)
            a.update(b'password')
            a.update(b'c3cef7c66a1843f8b3a9e6a1e3160e20')
            a.update(b'com.zhihu.web')
            a.update(str(int(time.time() * 1000)).encode('utf-8'))
            self.signature = a.hexdigest()
    
        def post_login_data(self):
            data = {
                'client_id': 'c3cef7c66a1843f8b3a9e6a1e3160e20',
                'grant_type': 'password',
                'timestamp': str(int(time.time() * 1000)),
                'source': 'com.zhihu.web',
                'signature': self.signature,
                'username': '+8618953675221',
                'password': '',
                'captcha': self.captcha,
                'lang': 'en',
                'utm_source': '',
                'ref_source': 'other_https://www.zhihu.com/signin?next=%2F',
            }
    
            headers = {
                'x-zse-83': '3_2.0',
                'content-type': 'application/x-www-form-urlencoded',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER',
            }
    
            data = urlencode(data)
            with open('zhih.js', 'rt', encoding='utf-8') as f:
                js = execjs.compile(f.read(), cwd='node_modules')
            data = js.call('b', data)
    
            r = self.session.post(url=self.login_api, headers=headers, data=data)
            print(r.text)
            if r.status_code == 201:
                self.session.cookies.save('mycookie')
                print('登录成功')
            else:
                print('登录失败')
    
        def login(self):
            self.get_base_cookie()
            self.deal_captcha()
            self.get_signature()
            self.post_login_data()
    if __name__ == '__main__':
        zhihu_spider = Spider()
        zhihu_spider.login()
    
    
    
    
    
    

    27 爬虫的反爬措施总结

    1 user-agent
    2 referer
    3 cookie(cookie池,先访问一次)
    4 频率限制(代理池,延迟)
    5 js加密(扣出来,exjs模块指向)
    6 css加密
    7 验证码(打码平台),半手动
    8 图片懒加载
    

    拓展

    宝塔:
    	https://github.com/aaPanel/BaoTa/
    jumpserver:
        https://github.com/jumpserver/jumpserver/
    聊天机器人:
    	https://www.cnblogs.com/liuqingzheng/articles/9079192.html
    http:
        https://juejin.im/post/6857287743966281736
        https://www.cnblogs.com/PythonLearner/p/13424051.html
    web服务端给浏览器发信息
    # 轮询和长轮询
    # websocket:channles(django作者写的)
    
  • 相关阅读:
    【转】C++ 实现线程池
    【转】C++ 实现reactor 模式
    【转】C++ 单例模式
    高并发的内存池——TLS MEMMORY POOL
    爬取某东商品数据
    查看解放号的一些Python岗
    查看前程无忧Python岗及一些可视化
    爬某牙小姐姐视频
    爬取某云音乐热歌榜
    键盘及鼠标记录程序
  • 原文地址:https://www.cnblogs.com/achai222/p/13525580.html
Copyright © 2011-2022 走看看