• 世界杯快到了,看我用Python爬虫实现(伪)球迷速成!


    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    还有4天就世界杯了,作为一个资深(伪)球迷,必须要实时关注世界杯相关新闻,了解各个球队动态,这样才能在一堆球迷中如(大)鱼(吹)得(特)水(吹),迎接大家仰慕的目光!

    给大家分享一个快速了解相关信息的办法:刷论坛!我们来一起做个虎扑论坛的爬虫吧!

    抓包获取虎扑论坛相关帖子内容,逐条显示!

    先来观察下网页,打开论坛首页,选择国际足球

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    然后往下拉,找到世界杯相关内容

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    这里就是我们的目标了,所有相关的新闻都会在这里显示,用F12打开“开发者工具”然后往下浏览看看数据包

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    注意箭头指向的那几个地方!

    这就是刚才浏览的新闻所在的json包,来看看具体数据是什么

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    ok,标题、地址、发布时间包括来源都已经出现了!我们可以直接抓取json数据然后取出相关内容!

    再进入具体新闻页面看看

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    所有的文本内容,都在<div class="artical-main-content">这个标签下的<p></p>标签内,我们可以用xpath直接取div下的所有文本内容!

    这里就不一 一说明了,直接上代码,并录个小的GIF图片给大家看看效果

     1 #Q群542110741
     2 # -*- coding:utf-8 -*-
     3 import requests
     4 from lxml import etree
     5 
     6 header = {
     7     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0',
     8     'Host':'soccer.hupu.com',
     9     'Referer':'https://soccer.hupu.com/'}
    10 i = 0
    11 while 1:
    12     #构建循环页面翻页
    13     url = 'https://soccer.hupu.com/home/latest-news?league=世界杯&page='
    14     i += 1
    15     #获取json数据,一页20个
    16     html = requests.get(url+str(i),headers=header).json()['result']
    17     for info in html:
    18         time_r = info['time']#发布时间
    19         title = info['title']#标题
    20         url_r = info['url']#新闻链接
    21         origin = info['origin']#来源
    22         print(title)
    23         print('发布时间:',time_r,' '*5,'来自:',origin)
    24         head = header
    25         head['Host'] = 'voice.hupu.com'#更改header中Host参数
    26         html_r = requests.get(url_r,headers=head)#获取新闻详情
    27         html_r.encoding = 'utf-8'#编码格式指定
    28         #获取div下的所有文本
    29         datas = etree.HTML(html_r.text).xpath('//div[@class="artical-content-read"]')[0].xpath('string(.)').strip()
    30         print('
    '+'内容:'+'
    '*2,datas,'
    ')
    31         #可由用户手动退出循环
    32         if input('任意键继续,“q”退出') in ['q', 'Q']:
    33             exit()

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    现在我们可以快乐的刷刷论坛,积累最新资讯,秒杀一切挡在我们前(装)进(B)道路上的渣渣吧~!

    世界杯快到了,看我用Python爬虫实现(伪)球迷速成!

     

    欢迎大家关注,私信我一起学习,一起看球!

  • 相关阅读:
    virtualBox下面安装linux系统如何共享目录
    PHP中spl_autoload_register()函数
    PHP 5.5 新特性
    useradd密码无效
    Linux audit安全审计工具
    Javascript class获取回调函数数据
    RPi 3B 无线连接配置
    Refused to execute inline event handler because it violates the following Content Security Policy directive: "xxx". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...')
    options.html:1 Refused to load the script 'xxxx' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
    jQuery.Deferred exception: $.get is not a function TypeError: $.get is not a function
  • 原文地址:https://www.cnblogs.com/qun542110741/p/9163223.html
Copyright © 2020-2023  润新知