[Python爬虫] 之二十七:Selenium +phantomjs 利用 pyquery抓取今日头条视频
一、介绍
本例子用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抓取今日头条视频的更多相关文章
- [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据
一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...
- [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息
一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...
- [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目
一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...
- [Python爬虫] 之二十二:Selenium +phantomjs 利用 pyquery抓取界面网站数据
一.介绍 本例子用Selenium +phantomjs爬取界面(https://a.jiemian.com/index.php?m=search&a=index&type=news& ...
- [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/epg/index.shtml?date=2018-03-25)的信息 二.网站信息 三.数据抓 ...
- [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://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...
- [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息
一.介绍 本例子用Selenium +phantomjs爬取智能电视网站(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取图片信息. 给定关键字:数字:融合:电视 ...
随机推荐
- CentOS 7 部署nginx
**二进制安装 安装Nginx源 rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el ...
- strncpy 引起的思考,重新认识了strncpy这个函数【转】
转自:http://blog.csdn.net/edwardlulinux/article/details/47134513 版权声明:本文为博主原创文章,未经博主允许不得转载. 首先来看一个司空见惯 ...
- sprintf,snprintf的用法(可以作为linux中itoa函数的补充)【转】
转自:http://blog.csdn.net/educast/article/details/25068445 函数功能:把格式化的数据写入某个字符串 头文件:stdio.h 函数原型:int sp ...
- Python3 安装xlrd、xlwt、xlutils
Python版本3.4,装xlrd和xlwt和xlutils的时间:2017-09-07. 安装xlrd.xlwt.xlutils很简单,直接[pip install xlrd].[pip insta ...
- vim操作大全
# 转自 https://blog.csdn.net/weixin_37657720/article/details/80645991 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim ...
- 使用MybatisGenerator自动生成Model,Mapping和Mapper文件
Mybatis和Hibernate都是持久层框架,MyBatis出现的比Hibernate晚,这两种框架我都用过,对于二者的优势我的感触不深,个人感觉MyBatis自动生成model,Mapping, ...
- [BZOJ2667][cqoi2012]模拟工厂 贪心
2667: [cqoi2012]模拟工厂 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 367 Solved: 184[Submit][Status] ...
- 前端读者 | 别人写的css,你敢用吗?
本文来自@yeaseonzhang:链接:http://yeaseonzhang.github.io/2018/04/10/%E5%88%AB%E4%BA%BA%E5%86%99%E7%9A%84cs ...
- HDU 多校1.11
- 递归输入与引用传值(UVa839 Not so Mobile)
题目的大意是一个树形天平,输入给出样例的个数,然后空一行,每行4个数W1,D1,W2,D2,分别代表天平左侧的重量.力臂和天平右侧的重量.力臂.如果W1或者W2为0,则代表该节点有左子树或右子树,如果 ...