[Python爬虫] 之二十九:Selenium +phantomjs 利用 pyquery抓取节目信息信息
一、介绍
本例子用Selenium +phantomjs爬取节目(http://tv.cctv.com/epg/index.shtml?date=2018-03-25)的信息
二、网站信息


三、数据抓取
针对上面的网站信息,来进行抓取
1、首先抓取信息列表
抓取代码:Elements = doc('div[class="epglist"]').find('ul')
2、节目名称,链接,时间
title = subEle('div[class="innerbox"]').find('h3').text().encode('utf8')
link = subEle('div[class="innerbox"]').find('p').find('a').attr('href')
strTime = subEle('div[class="innerbox"]').find('p').text().encode('utf8')
四,实现代码
# coding=utf-8
import os
import re
from selenium import webdriver
from datetime import datetime,timedelta
import selenium.webdriver.support.ui as ui
import time
from pyquery import PyQuery as pq
class cctvDriver: def __init__(self,startDate,endDate):
#通过配置文件获取IEDriverServer.exe路径
self.urls = self.getUrlsFromStartEndDate(startDate,endDate)
IEDriverServer ='C:\Program Files\Internet Explorer\IEDriverServer.exe'
self.driver = webdriver.Ie(IEDriverServer)
self.driver.maximize_window()
self.fileName = time.strftime('%Y-%m-%d') def compareDate(self, startDate, endDate):
start_Date = time.strptime(startDate, "%Y-%m-%d")
end_Date = time.strptime(endDate, "%Y-%m-%d")
totalSeconds = (end_Date - start_Date).total_seconds()
if totalSeconds >= 0:
print endDate
return True
else:
print startDate
return False def compareTime(self, startTime, endTime):
st = int(startTime.replace(':',""))
et = int(endTime.replace(':',""))
if st>et:
return True
else:
return False def getUrlsFromStartEndDate(self,startDate,endDate): urls = []
start_Date = datetime.strptime(startDate, "%Y-%m-%d")
end_date = datetime.strptime(endDate, "%Y-%m-%d")
ts = end_date-start_Date days = ts.days + 1
index = 0
for d in xrange(0,days):
date = start_Date + timedelta(days=index)
urls.append('http://tv.cctv.com/epg/index.shtml?date='+date.strftime("%Y-%m-%d"))
index += 1
return urls def WriteLog(self, message,date):
fileName = os.path.join(os.getcwd(), 'cctvInfo/'+date + '.txt')
with open(fileName, 'a') as f:
f.write(message) def CatchData(self):
className = "//div[@class='epglist']/ul"
for url in self.urls:
date = url.split('=')[1]
start_Date = datetime.strptime(date, "%Y-%m-%d") + timedelta(days=-1)
predate = start_Date.strftime("%Y-%m-%d")
self.driver.get(url)
time.sleep(5)
selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
doc = pq(selenium_html)
Elements = doc('div[class="epglist"]').find('ul')
message = ''
recount = 0
for element in Elements.items():
channel = element.attr('id')
subElements = element.find("li") for subEle in subElements.items():
strTime = subEle('div[class="innerbox"]').find('p').text().encode('utf8').strip().replace(
'回看', '').replace('直播','')
if strTime:
title = subEle('div[class="innerbox"]').find('h3').text().encode(
'utf8').strip().replace(
',', ',')
link = subEle('div[class="innerbox"]').find('p').find('a').attr('href')
if self.compareTime(strTime.split('~')[0],strTime.split('~')[1]):
starttime = predate + " " + strTime.split('~')[0]
else:
starttime = date + " " + strTime.split('~')[0]
endtime = date + " " + strTime.split('~')[1] mess = '\r\n{0},{1},{2},{3},{4}'.format(channel, title, starttime, endtime, link)
# print mess
message += mess
recount+=1
if len(message)>10:
self.WriteLog(message.strip(),date)
print recount
self.driver.close()
self.driver.quit() # #测试抓取微博数据
obj = cctvDriver('2018-01-01','2018-03-01')
obj.CatchData()
[Python爬虫] 之二十九:Selenium +phantomjs 利用 pyquery抓取节目信息信息的更多相关文章
- [Python爬虫] 之二十七:Selenium +phantomjs 利用 pyquery抓取今日头条视频
一.介绍 本例子用Selenium +phantomjs爬取今天头条视频(http://www.tvhome.com/news/)的信息,输入给定关键字抓取图片信息. 给定关键字:视频:融合:电视 二 ...
- [Python爬虫] 之二十三:Selenium +phantomjs 利用 pyquery抓取智能电视网数据
一.介绍 本例子用Selenium +phantomjs爬取智能电视网(http://news.znds.com/article/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字 ...
- [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据
一.介绍 本例子用Selenium +phantomjs爬取36氪网站(http://36kr.com/search/articles/电视?page=1)的资讯信息,输入给定关键字抓取资讯信息. 给 ...
- [Python爬虫] 之二十:Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据
一.介绍 本例子用Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据()的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯 ...
- [Python爬虫] 之二十八:Selenium +phantomjs 利用 pyquery抓取网站排名信息
一.介绍 本例子用Selenium +phantomjs爬取中文网站总排名(http://top.chinaz.com/all/index.html,http://top.chinaz.com/han ...
- [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目
一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...
- [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息
一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...
- [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息
一.介绍 本例子用Selenium +phantomjs爬取智能电视网站(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取图片信息. 给定关键字:数字:融合:电视 ...
- [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据
一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...
随机推荐
- CCF试题:高速公路(Targin)
问题描述 某国有n个城市,为了使得城市间的交通更便利,该国国王打算在城市之间修一些高速公路,由于经费限制,国王打算第一阶段先在部分城市之间修一些单向的高速公路. 现在,大臣们帮国王拟了一个修高速公路的 ...
- hdu 3078(LCA的在线算法)
Network Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- System.Web.HttpContext.Current.Request用法
public static void SetRegisterSource() { if (System.Web.HttpContext.Current.Request["website&qu ...
- 关于在C#中对函数重载理解
函数重载是个什么概念,才接触的这个概念的时候我也是完全昏了,还在自己看看了书后就理解了.那什么是函数重载呢?我个人理解的是在同一个作用域下有多个同名的函数,但是他们的形参的类型是不同的,或者参数个数是 ...
- [mysql] 删除唯一约束unique
alter table ot_document drop index title
- 剑指offer-链表中倒数第 K 个结点
输入一个链表,输出该链表中倒数第k个结点. /* public class ListNode { int val; ListNode next = null; ListNode(int val) { ...
- 洛谷P2751[USACO]工序安排
题目传送门 怎么说呢,这个题目我刚开始随便乱搞了几下,交了个暴力代码上去居然还水了49分,数据确实有点弱啊,然后看到洛谷上那位大佬Redbag的题解瞬间就佩服的五体投地,那真的是简洁.易懂又高效.直接 ...
- Spring Cloud Feign 总结
Spring Cloud中, 服务又该如何调用 ? 各个服务以HTTP接口形式暴露 , 各个服务底层以HTTP Client的方式进行互相访问. SpringCloud开发中,Feign是最方便,最为 ...
- HashMap的实现原理 HashMap底层实现,hashCode如何对应bucket?
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 数组和链表组合成的链表散列结构,通过hash算法,尽量将数组中的数据分布均匀,如果has ...
- [BZOJ3456]城市规划(生成函数+多项式求逆+多项式求ln)
城市规划 时间限制:40s 空间限制:256MB 题目描述 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一 ...