[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.jpgif 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 = 0for link in self.start_urls:self.driver.get(link)keyword = self.keyword_list[index]index = index + 1time.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()
转载于:https://www.cnblogs.com/shaosks/p/7070126.html
[Python爬虫] 之二十七:Selenium +phantomjs 利用 pyquery抓取今日头条视频相关推荐
- [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抓取电视之家网数据
一.介绍 本例子用Selenium +phantomjs爬取电视之家(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓 ...
- Python爬虫实战02:分析Ajax请求并抓取今日头条街拍
1 目标网站分析 首先我们打开今日头条网站,搜索 街拍,点击图集,这里每就是我们要爬取的目录,我们称为索引页.1 点开一个标题,进去,称为详情页.2这里面的图是我们所要爬取的.比如这里可以点击图片,共 ...
- python爬虫成长之路(一):抓取证券之星的股票数据
python爬虫成长之路(一):抓取证券之星的股票数据 获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一.鉴于此,我拾起了Python这把利器,开启了网络爬虫之路. 本篇 ...
- Python爬虫之XPath基础教程:用代码抓取网页数据
Python爬虫之XPath基础教程:用代码抓取网页数据 在网络时代,网页数据是获取信息和进行分析的最重要的来源之一.Python的爬虫技术让我们可以轻松抓取网页数据,并进行数据处理.XPath是一种 ...
- 利用Ajax爬取今日头条头像,街拍图片。关于崔庆才python爬虫爬取今日头条街拍内容遇到的问题的解决办法。
我也是初学爬虫,在看到崔庆才大佬的爬虫实战:爬取今日头条街拍美图时,发现有些内容过于陈旧运行程序时已经报错,网页的源代码早已不一样了.以下是我遇到的一些问题. 1.用开发者选项筛选Ajax文件时预览看 ...
- python 模拟浏览器selenium_Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息示例...
本文实例讲述了Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息.分享给大家供大家参考,具体如下: import time from multiprocessing import Poo ...
- 用python爬取今日头条上的图片_Python爬虫:抓取今日头条图集
今天我们来爬取今日头条图集,老司机以街拍为例. 运行平台: Windows Python版本: Python3.6 IDE: Sublime Text 其他工具: Chrome浏览器 1.网页分析 从 ...
最新文章
- G1调优很难?记住这些经验技巧~
- SAP WMSD集成之Copy WM Quantity – Not Copy WM qty as delivery qty into delivery But PGI
- 详解:XenServer丢失存储库SR解决方法
- python语言入门m-python基础入门这一篇就够
- java获取eureka_Spring Cloud服务发现:Eureka客户端
- 【Java4】实例初始化,类初始化,/接口,多态,final/static,权限修饰符/native
- [NOIP2014]联合权值
- android中解析后台返回的json字符串
- 前端老弟第一次写后端,崩了!
- JavaScript技巧[转载]
- 浅谈Spring中JDK动态代理与CGLIB动态代理
- 如何用 Python 快速开发一个区块链数据结构?
- JDBC的数据库的基础事务管理
- Review of Classic Clustering Algorithms
- eclipse更换jdk版本
- qlib里alpha158因子库的计算与缓存
- 海量的超赞 Linux 软件 (转载)
- Vue中使用Tinymce-edtio
- mac 版 Goland 使用教程一
- hasattr()函数的用法