[Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息
一、介绍
本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标题,时间)
二、网站信息


python 代码
# coding=utf-8
import os
import re
from selenium import webdriver
from datetime import datetime,timedelta
import time
from pyquery import PyQuery as pq
import re
import mongoDB
import datetime class consumer: def __init__(self):
#通过配置文件获取IEDriverServer.exe路径
# IEDriverServer ='C:\Program Files\Internet Explorer\IEDriverServer.exe'
# self.driver = webdriver.Ie(IEDriverServer)
# self.driver.maximize_window()
self.driver = webdriver.PhantomJS(service_args=['--load-images=false'])
# self.driver = driver = webdriver.Chrome()
self.driver.set_page_load_timeout(10)
self.driver.maximize_window()
self.db = mongoDB.mongoDbBase() def WriteLog(self, message,date):
fileName = os.path.join(os.getcwd(), 'consumer/' + date + '.txt')
with open(fileName, 'a') as f:
f.write(message)
# http://search.cctv.com/search.php?qtext=消费主张&type=video
def CatchData(self,url='http://search.cctv.com/search.php?qtext=%E6%B6%88%E8%B4%B9%E4%B8%BB%E5%BC%A0&type=video'):
error = ''
try:
self.driver.get(url)
time.sleep(1)
selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
doc = pq(selenium_html) filename = datetime.datetime.now().strftime('%Y-%m-%d')
message = '{0},{1}'.format( '标题', '时间')
filename = datetime.datetime.now().strftime('%Y-%m-%d')
self.WriteLog(message, filename)
pages = doc("div[class='page']").find("a")
# 2018-06-05 00:12:21
pattern = re.compile("\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}")
for index in range(1,6):
url = "get_data('{0}', '消费主张', 'relevance', 'video', '-1', '1', '', '20', '1')".format(index) self.driver.execute_script(url)
selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
doc = pq(selenium_html) Elements = doc("div[class='jvedio']").find("a")
for sub in Elements.items():
title = sub.attr('title').encode('utf8')
ts = pattern.findall(title)
strtime = ''
if ts and len(ts) == 1:
strtime = ts[0]
if strtime:
index = title.index(strtime)
title = title[0:index]
title = '\n{0},{1}'.format(title,strtime)
self.WriteLog(title, filename) except Exception, e1:
error = e1.message # def CatchData(self,url='http://search.cctv.com/search.php?qtext=%E6%B6%88%E8%B4%B9%E4%B8%BB%E5%BC%A0&type=video'):
# error = ''
# try:
# self.driver.get(url)
# time.sleep(1)
# selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
# doc = pq(selenium_html)
#
# filename = datetime.datetime.now().strftime('%Y-%m-%d')
#
# pages = doc("div[class='page']").find("a")
#
# for element in pages.items():
# url = element.attr('onclick').encode('utf8')
# # get_data('1','消费主张','relevance','video','-1','1','','20','1')
# # get_data('2', '消费主张', 'relevance', 'video', '-1', '1', '', '20', '1')
# print url
# self.driver.execute_script(url)
# selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
# doc = pq(selenium_html)
#
# Elements = doc("div[class='jvedio']").find("a")
# for sub in Elements.items():
# title = sub.attr('title').encode('utf8')
# print title
# title = '\n{0}'.format(title)
# self.WriteLog(title, filename)
# except Exception, e1:
# error = e1.message obj = consumer() obj.CatchData()
# obj.CatchContent('')
# obj.export('')
[Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息的更多相关文章
- [Python爬虫] 之二十八:Selenium +phantomjs 利用 pyquery抓取网站排名信息
一.介绍 本例子用Selenium +phantomjs爬取中文网站总排名(http://top.chinaz.com/all/index.html,http://top.chinaz.com/han ...
- [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据
一.介绍 本例子用Selenium +phantomjs爬取36氪网站(http://36kr.com/search/articles/电视?page=1)的资讯信息,输入给定关键字抓取资讯信息. 给 ...
- [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目
一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...
- [Python爬虫] 之十六:Selenium +phantomjs 利用 pyquery抓取一点咨询数据
本篇主要是利用 pyquery来定位抓取数据,而不用xpath,通过和xpath比较,pyquery效率要高. 主要代码: # coding=utf-8 import os import re fro ...
- [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据
一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...
- [Python爬虫] 之二十二:Selenium +phantomjs 利用 pyquery抓取界面网站数据
一.介绍 本例子用Selenium +phantomjs爬取界面(https://a.jiemian.com/index.php?m=search&a=index&type=news& ...
- [Python爬虫] 之二十九:Selenium +phantomjs 利用 pyquery抓取节目信息信息
一.介绍 本例子用Selenium +phantomjs爬取节目(http://tv.cctv.com/epg/index.shtml?date=2018-03-25)的信息 二.网站信息 三.数据抓 ...
- [Python爬虫] 之十七:Selenium +phantomjs 利用 pyquery抓取梅花网数据
一.介绍 本例子用Selenium +phantomjs爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...
- [Python爬虫] 之二十七:Selenium +phantomjs 利用 pyquery抓取今日头条视频
一.介绍 本例子用Selenium +phantomjs爬取今天头条视频(http://www.tvhome.com/news/)的信息,输入给定关键字抓取图片信息. 给定关键字:视频:融合:电视 二 ...
随机推荐
- 在C#中使用CURL
private string args = ""; /// <summary> /// 参数 /// </sum ...
- 洛谷P3834 [模板]可持久化线段树1(主席树) [主席树]
题目传送门 可持久化线段树1(主席树) 题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定 ...
- JavaWeb 之 AJAX
Ajax ajax:AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新 AJAX:Asynchronous JavaScript and XML,异步 javasc ...
- iOS 9的新的改变 iOS SDK Release Notes for iOS 9 说了些改变
iOS 9的新的改变 iOS SDK Release Notes for iOS 9 说了些改变 看了下还算能理解!!!有兴趣可以看看哈!!!不喜勿喷!!后面的对于废除的方法什么有用感觉!!! ...
- C和指针之学习笔记(5)
第10章 使用结构和指针 单链表 typedef struct NODE { struct NODE *link; int value; } Node; 插入到一个有序单链表: #include< ...
- java8新特性——四大内置核心函数式接口
在前面几篇简单介绍了一些Lambda表达式得好处与语法,我们知道使用Lambda表达式是需要使用函数式接口得,那么,岂不是在我们开发过程中需要定义许多函数式接口,其实不然,java8其实已经为我们定义 ...
- 又见Python<2>:如何安装第三方库(Windows)
使用python进行数据分析或者数据处理时,往往需要使用一些库,而使用库之前必须安装它.Anaconda内置了很多常用的第三方库,可以满足绝大部分需求,比如numpy.pandas.matplotli ...
- 平衡树之treap luoguP3369
今天又复习了一遍treap,这题有前驱后继排名排位添加和删除等操作. 非常好写,虽然代码颇长但逻辑性很强. #include<bits/stdc++.h> using namespace ...
- [BZOJ2431][HAOI2009]逆序对数列(DP)
从小到大加数,根据加入的位置转移,裸的背包DP. #include<cstdio> #include<cstring> #include<algorithm> #d ...
- [SimpleOJ233]a xor b
题目大意: 给你一个数列,求所有区间最大值和次大只异或的最大值. 思路: 很容易想到一个O(n^2)的暴力. O(n)的单调栈做法似乎也很好想,不过考场上没想出来. 对于数列上的某一个数,我们维护在它 ...