找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 40254|回复: 295

贡献个EPG获取的爬虫,开源

 火... [复制链接]
本帖最后由 3mile 于 2019-10-23 13:51 编辑

从tianmaotv获取,代码开源。各位自由发挥
首次使用python
设置了代码回复可见,只为统计下载量
论坛的hide代码有问题的,代码框140强行加入了【hide】。各位自行删除【hide】吧。
修改成下图的样子

代码如下
  1. import sys
  2. import requests
  3. import re
  4. import time
  5. import codecs
  6. import json
  7. import xml.etree.ElementTree as ET
  8. from datetime import datetime, date, time, timedelta
  9. from bs4 import BeautifulSoup
  10. from xml.dom import minidom
  11. from xpinyin import Pinyin


  12. if sys.version_info[0] == 2:
  13.     from urllib2 import urlopen  # Python 2
  14. else:
  15.     from urllib.request import urlopen  # Python3

  16. def get_week_backup():
  17.     now=datetime.now()
  18.     week=now.strftime('%w')
  19.     if int(week) == 7:
  20.         w=[week,str(1)]
  21.     else:
  22.         w=[week,str(int(week)+1)]
  23.     return w

  24. def get_week():
  25.     now=datetime.now()
  26.     week=now.strftime('%w')
  27.     wd=int(week)
  28.     w=[]
  29.     for i in range(wd,8):
  30.         w.append(str(i))
  31.     return w

  32. def saveXML(root, filename, indent="\t", newl="\n", encoding="utf-8"):
  33.     rawText = ET.tostring(root)
  34.     dom = minidom.parseString(rawText)
  35.     with codecs.open(filename, 'w', 'utf-8') as f:
  36.         #writer = codecs.lookup('utf-8')[3](f)
  37.         dom.writexml(f, "", indent, newl, encoding)

  38. def ch_dict():
  39.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
  40.                    'Connection': 'keep-alive', 'Cache-Control': 'no-cache'}
  41.     url="https://www.tianmaotv.cn/tv/"
  42.     ch_list=["type_101","type_102","tv_453","tv_526"]
  43.     dict_url={}
  44.     dict_name={}
  45.     p=Pinyin()
  46.     tmp_dic={}

  47.     for ch in ch_list:
  48.         r = requests.get(url+ch, headers=headers)               
  49.         r.encoding=r.apparent_encoding
  50.         soup = BeautifulSoup(r.text, 'lxml')
  51.         
  52.         list_program_div = soup.find(
  53.                 name='dl', attrs={"class": "tv-station-list"}).find_all('a')
  54.         
  55.         for tagprogram in list_program_div:
  56.             r=re.compile(r"\/jmb")
  57.             if r.match(tagprogram["href"]):
  58.                 dict_url[tagprogram.string]=tagprogram["href"]
  59.                 tmp=p.get_initials(tagprogram.string, u'')
  60.                 #flages=tmp is tmp_dic
  61.                 if tmp in tmp_dic:
  62.                     tmp_dic[tmp+"1"]=tagprogram.string   
  63.                 else:
  64.                     tmp_dic[tmp]=tagprogram.string               
  65.                 '''
  66.                 name_list_temp=list(dict_name.values())
  67.                 flags=tagprogram.string in dict_name
  68.                 if not flags:
  69.                     dict_name[tagprogram.string]=p.get_initials(tagprogram.string, u'')
  70.                 for ss in name_list_temp:
  71.                     if tmp == ss:
  72.                         print(tmp,tmp+"1",tagprogram.string)
  73.                         dict_name[tagprogram.string]=p.get_initials(tagprogram.string, u'')+"1"
  74.                         #print(dict_name)'''
  75.     dict_name={v:k for k, v in tmp_dic.items()}
  76.     return dict_url,dict_name

  77. def get_channel_programme(url_name_py,real_rul):
  78.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
  79.                'Connection': 'keep-alive', 'Cache-Control': 'no-cache'}
  80.    
  81.     r = requests.get("https://www.tianmaotv.cn"+real_rul, headers=headers)
  82.     r.encoding=r.apparent_encoding
  83.     soup = BeautifulSoup(r.text, 'lxml')
  84.     #print(soup)

  85.     timestart={}
  86.     timestop={}
  87.     channel_title={}

  88.     list_program_div = soup.find(name='dl', attrs={"class": "program-list"}).find_all('dd')
  89.     #print(list_program_div)
  90.     for i in list_program_div:
  91.         times=i.time.string.strip()
  92.         times=datetime.strptime("2019-"+times,'%Y-%m-%d %H:%M')
  93.         timestart[times.strftime("%Y%m%d%H%M%S")]=i.contents[2].strip()
  94.     return timestart


  95. aa=ch_dict()
  96. channel_url=aa[0]
  97. channel_py=aa[1]
  98. '''
  99. with open('e:\\python\\tianmaotv\\data.json', 'w',encoding='utf-8') as f:
  100.     json.dump(aa, f,sort_keys=False, indent=4,  separators=(',',':'),ensure_ascii=False)

  101. print(channel_py.items)
  102. print(channel_url.items)
  103. '''
  104. new_root = ET.Element('tv')
  105. new_root.set("generator-info-name", "3mile")
  106. new_root.set("generator-info-url", "https://3mile.top")
  107. new_root.tail="\n"
  108. for k,v in channel_py.items():
  109.     child = ET.SubElement(new_root,"channel")
  110.     #child.tail="\n"
  111.     child.set("id",v)
  112.     child_name=ET.SubElement(child,"display-name")
  113.     #child.tail="\n"
  114.     child_name.set("lang","zh")
  115.     child_name.text=k
  116.     child_url=ET.SubElement(child,'url')
  117.     #child_url.tail='\n'
  118.     child_url.text="https://tianmao.tv"+channel_url[k]

  119. #ET.dump(new_root)
  120. wday=get_week()
  121. for k,v in channel_url.items():
  122.     programme={}
  123.     print("正在获取 "+k+" EPG信息")
  124.     for i in wday:
  125.         url=re.sub(r'\/, "", v)+"_w"+i+"/"
  126.         progr=get_channel_programme(channel_py[k],url)
  127.         programme.update(progr)
  128.    
  129.     tmp=list(programme.items())
  130.     list_time=list(programme.keys())
  131.     list_title=list(programme.values())

  132.     for t in range(len(list_time)):
  133.         tit=list_title[t]
  134.         begin=list_time[t]
  135.         if t+1<len(list_time):
  136.             end=list_time[t+1]
  137.         else:
  138.             ta1=datetime.strptime(list_time[t],'%Y%m%d%H%M%S')
  139.             ta1=ta1+timedelta(hours=1)
  140.             end=ta1.strftime('%Y%m%d%H%M%S')

  141.         programme_sub=ET.SubElement(new_root,"programme")
  142.         #programme_sub.tail='\n'
  143.         programme_sub.set("start",begin+" +0800")
  144.         programme_sub.set("stop",end+" +0800")
  145.         programme_sub.set("channel",channel_py[k])
  146.         
  147.         programme_title=ET.SubElement(programme_sub,"title")
  148.         programme_title.set("lang","zh")
  149.         programme_title.text=tit
  150.     print("已经获取 "+k+"EPG内容")

  151. saveXML(new_root,"e.xml")
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
可以像原来超级直播自定义那样方便使用吗?
回复

使用道具 举报

多少个多少如果洞若观火突然很坦然发货
回复

使用道具 举报


可以像原来超级直播自定义那样方便使用吗?
回复

使用道具 举报

贡献个EPG获取的爬虫,开源
回复

使用道具 举报

思考思考了
回复

使用道具 举报

贡献个EPG获取的爬
回复

使用道具 举报

谢谢分享,这个不错的样子
回复

使用道具 举报

来自手机 | 显示全部楼层
分享万岁!谢谢啊!
回复

使用道具 举报

谢谢分享。。。。
回复

使用道具 举报

谢谢楼主分享
回复

使用道具 举报

谢谢楼主分享
回复

使用道具 举报

谢谢楼主分享EPG获取的爬虫
回复

使用道具 举报

谢谢大佬分享
回复

使用道具 举报

太好了!实在好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-5-22 12:28

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表