一、介绍

    本例子用Selenium +phantomjs爬取今天头条视频(http://www.tvhome.com/news/)的信息,输入给定关键字抓取图片信息。

    给定关键字:视频;融合;电视

   

 

  二、网站信息

    

    

 

  三、数据抓取

    针对上面的网站信息,来进行抓取

    1、首先抓取视频信息列表

      抓取代码:Elements = doc('div[class="articleCard"]')

    2、抓取图片

      视频url:url = 'http://www.toutiao.com' + element.find('a[class="link title"]').attr('href')

          videourl = dochtml('video[class="vjs-tech"]').find('source').attr('src')

  

  四、完整代码

# coding=utf-8
import os
import re
from selenium import webdriver
import selenium.webdriver.support.ui as ui
import time
from datetime import datetime
import IniFile
# from threading import Thread
from pyquery import PyQuery as pq
import LogFile
import mongoDB
import urllib
class toutiaoSpider(object):
def __init__(self): logfile = os.path.join(os.path.dirname(os.getcwd()), time.strftime('%Y-%m-%d') + '.txt')
self.log = LogFile.LogFile(logfile)
configfile = os.path.join(os.path.dirname(os.getcwd()), 'setting.conf')
cf = IniFile.ConfigFile(configfile)
webSearchUrl = cf.GetValue("toutiao", "webSearchUrl")
self.keyword_list = cf.GetValue("section", "information_keywords").split(';')
self.db = mongoDB.mongoDbBase()
self.start_urls = [] for word in self.keyword_list:
self.start_urls.append(webSearchUrl + urllib.quote(word)) self.driver = webdriver.PhantomJS()
self.wait = ui.WebDriverWait(self.driver, 2)
self.driver.maximize_window() def down_video(self, videourl):
"""
下载视频到本地
:param videourl: 视频url
"""
# http://img.tvhomeimg.com/uploads/2017/06/23/144910c41de4781ccfe9435e736ef72b.jpg
if len(videourl) > 0:
fileName = ''
if videourl.rfind('/') > 0:
fileName = time.strftime('%Y%m%d%H%M%S') + '.mp4'
u = urllib.urlopen(videourl)
data = u.read() strpath = os.path.join(os.path.dirname(os.getcwd()), 'video')
with open(os.path.join(strpath, fileName), 'wb') as f:
f.write(data) def scrapy_date(self):
strsplit = '------------------------------------------------------------------------------------'
index = 0
for link in self.start_urls:
self.driver.get(link) keyword = self.keyword_list[index]
index = index + 1
time.sleep(1) #数据比较多,延迟下,否则会出现查不到数据的情况 selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
doc = pq(selenium_html)
infoList = []
self.log.WriteLog(strsplit)
self.log_print(strsplit) Elements = doc('div[class="articleCard"]') for element in Elements.items():
url = 'http://www.toutiao.com' + element.find('a[class="link title"]').attr('href')
infoList.append(url)
if len(infoList)>0:
for url in infoList:
self.driver.get(url)
htext = self.driver.execute_script("return document.documentElement.outerHTML")
dochtml = pq(htext)
videourl = dochtml('video[class="vjs-tech"]').find('source').attr('src')
if videourl:
self.down_video(videourl) self.driver.close()
self.driver.quit() obj = toutiaoSpider()
obj.scrapy_date()

[Python爬虫] 之二十七:Selenium +phantomjs 利用 pyquery抓取今日头条视频的更多相关文章

  1. [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据

    一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...

  2. [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息

    一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...

  3. [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目

    一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...

  4. [Python爬虫] 之二十二:Selenium +phantomjs 利用 pyquery抓取界面网站数据

    一.介绍 本例子用Selenium +phantomjs爬取界面(https://a.jiemian.com/index.php?m=search&a=index&type=news& ...

  5. [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据

    一.介绍 本例子用Selenium +phantomjs爬取36氪网站(http://36kr.com/search/articles/电视?page=1)的资讯信息,输入给定关键字抓取资讯信息. 给 ...

  6. [Python爬虫] 之二十九:Selenium +phantomjs 利用 pyquery抓取节目信息信息

    一.介绍 本例子用Selenium +phantomjs爬取节目(http://tv.cctv.com/epg/index.shtml?date=2018-03-25)的信息 二.网站信息 三.数据抓 ...

  7. [Python爬虫] 之二十八:Selenium +phantomjs 利用 pyquery抓取网站排名信息

    一.介绍 本例子用Selenium +phantomjs爬取中文网站总排名(http://top.chinaz.com/all/index.html,http://top.chinaz.com/han ...

  8. [Python爬虫] 之十七:Selenium +phantomjs 利用 pyquery抓取梅花网数据

    一.介绍 本例子用Selenium +phantomjs爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...

  9. [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息

    一.介绍 本例子用Selenium +phantomjs爬取智能电视网站(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取图片信息. 给定关键字:数字:融合:电视 ...

随机推荐

  1. shell整数加法

    http://blog.csdn.net/ll_0520/article/details/5959577 #plus #!/bin/sh let a=$1+$2 b=$[$1+$2] ((c=$1+$ ...

  2. 视频图像处理基础知识5(RGB与Ycbcr相互转换公式 )【转】

    转自:http://blog.csdn.net/Times_poem/article/details/51471438 版权声明:本文为博主原创文章,未经博主允许不得转载. 需求说明:视频处理算法基本 ...

  3. glib windows下编译

    记录的比较粗糙,但是绝对可行的 一些小的瑕疵以后再解决吧 (android版本的过几天再贴,移植到android已经通过) msys+mingw包下载: http://sourceforge.net/ ...

  4. 1.tornado实现高并发爬虫

    from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...

  5. django框架实现restful风格的API开发

    RESTful风格的要求:https://www.cnblogs.com/chichung/p/9933116.html 利用django原生的框架直接做RESTful的API开发是怎样的呢?感受一下 ...

  6. 【转.解析清晰】你真明白 Python 装饰器么?

      原文出处: 武沛齐    装饰器是程序开发中经常会用到的一个功能,用好了装饰器,开发效率如虎添翼,所以这也是Python面试中必问的问题,但对于好多小白来讲,这个功能 有点绕,自学时直接绕过去了, ...

  7. (10)python 特殊方法

    一.构造方法 在类中定义构造函数 >>> class a: def __init__(self): self.age=42 >>> f=a() >>&g ...

  8. ZOJ 3380 Patchouli's Spell Cards

    方案数,$dp$. 总的方案数有$n^m$种,符合要求的直接算不好算,可以算反面,即不符合要求的. 设$dp[i][j]$表示前$i$种等级填了$j$个位置,那么$dp[i][j]=sum(dp[i- ...

  9. python3爬虫爬取网页思路及常见问题(原创)

    学习爬虫有一段时间了,对遇到的一些问题进行一下总结. 爬虫流程可大致分为:请求网页(request),获取响应(response),解析(parse),保存(save). 下面分别说下这几个过程中可以 ...

  10. 洛谷——P2371 EXCEEDED WARNING C(50)

    P2371 EXCEEDED WARNING C 题目背景 第三道溢出警告... 机(wei)智(suo)的TMXi又搬题来坑大家了... 注意时空限制[坏笑] 题目描述 [li]1949年,阿三的数 ...