具体代码如下:

  

# coding=utf-8
import os
import re
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import IniFile
from selenium.webdriver.common.keys import Keys
from threading import Thread
import thread
import LogFile
import urllib
import mongoDB
#抓取数据线程类
class ScrapyData_Thread(Thread):
#抓取数据线程类
def __init__(self,webSearchUrl,inputTXTIDLabel,searchlinkIDLabel,htmlLable,htmltime,keyword,invalid_day,db):
'''
构造函数
:param webSearchUrl: 搜索页url
:param inputTXTIDLabel: 搜索输入框的标签
:param searchlinkIDLabel: 搜索链接的标签
:param htmlLable: 要搜索的标签
:param htmltime: 要搜索的时间
:param invalid_day: 要搜索的关键字,多个关键字中间用分号(;)隔开
:param keywords: #资讯发布的有效时间,默认是3天以内
:param db: 保存数据库引擎
'''
Thread.__init__(self) self.webSearchUrl = webSearchUrl
self.inputTXTIDLabel = inputTXTIDLabel
self.searchlinkIDLabel = searchlinkIDLabel
self.htmlLable = htmlLable
self.htmltime = htmltime
self.keyword = keyword
self.invalid_day = invalid_day
self.db = db self.driver = webdriver.PhantomJS()
self.wait = ui.WebDriverWait(self.driver, 20)
self.driver.maximize_window() def Comapre_to_days(self,leftdate, rightdate):
'''
比较连个字符串日期,左边日期大于右边日期多少天
:param leftdate: 格式:2017-04-15
:param rightdate: 格式:2017-04-15
:return: 天数
'''
l_time = time.mktime(time.strptime(leftdate, '%Y-%m-%d'))
r_time = time.mktime(time.strptime(rightdate, '%Y-%m-%d'))
result = int(l_time - r_time) / 86400
return result def run(self):
print '关键字:%s' % self.keyword self.driver.get(self.webSearchUrl)
time.sleep(1) # js = "var obj = document.getElementById('ctl00_ContentPlaceHolder1_txtSearch');obj.value='" + self.keyword + "';"
# self.driver.execute_script(js)
# 点击搜索链接
ss_elements = self.driver.find_element_by_id(self.inputTXTIDLabel)
ss_elements.send_keys(unicode(self.keyword,'utf8')) search_elements = self.driver.find_element_by_id(self.searchlinkIDLabel)
search_elements.click()
time.sleep(4) self.wait.until(lambda driver: self.driver.find_elements_by_xpath(self.htmlLable))
Elements = self.driver.find_elements_by_xpath(self.htmlLable) timeElements = self.driver.find_elements_by_xpath(self.htmltime)
urlList = []
for hrefe in Elements:
urlList.append(hrefe.get_attribute('href').encode('utf8')) index = 0
strMessage = ' '
strsplit = '\n------------------------------------------------------------------------------------\n'
index = 0
# 每页中有用记录
recordCount = 0
usefulCount = 0
meetingList = []
kword = self.keyword
currentDate = time.strftime('%Y-%m-%d') for element in Elements: strDate = timeElements[index].text.encode('utf8')
# 日期在3天内并且搜索的关键字在标题中才认为是复合要求的数据
#因为搜索的记录是安装时间倒序,所以如果当前记录的时间不在3天以内,那么剩下的记录肯定是小于当前日期的,所以就退出
if self.Comapre_to_days(currentDate,strDate) < self.invalid_day:
usefulCount = 1
txt = element.text.encode('utf8')
if txt.find(kword) > -1:
dictM = {'title': txt, 'date': strDate,
'url': urlList[index], 'keyword': kword, 'info': txt}
meetingList.append(dictM) # print ' '
# print '标题:%s' % txt
# print '日期:%s' % strDate
# print '资讯链接:' + urlList[index]
# print strsplit # # log.WriteLog(strMessage)
recordCount = recordCount + 1
else:
usefulCount = 0 if usefulCount:
break
index = index + 1 self.db.SaveMeetings(meetingList)
print "共抓取了: %d 个符合条件的资讯记录" % recordCount self.driver.close()
self.driver.quit() if __name__ == '__main__': configfile = os.path.join(os.getcwd(), 'chinaMedia.conf')
cf = IniFile.ConfigFile(configfile)
webSearchUrl = cf.GetValue("section", "webSearchUrl")
inputTXTIDLabel = cf.GetValue("section", "inputTXTIDLabel")
searchlinkIDLabel = cf.GetValue("section", "searchlinkIDLabel")
htmlLable = cf.GetValue("section", "htmlLable")
htmltime = cf.GetValue("section", "htmltime") invalid_day = int(cf.GetValue("section", "invalid_day")) keywords= cf.GetValue("section", "keywords")
keywordlist = keywords.split(';')
start = time.clock()
db = mongoDB.mongoDbBase()
for keyword in keywordlist:
if len(keyword) > 0:
t = ScrapyData_Thread(webSearchUrl,inputTXTIDLabel,searchlinkIDLabel,htmlLable,htmltime,keyword,invalid_day,db)
t.setDaemon(True)
t.start()
t.join() end = time.clock()
print "整个过程用时间: %f 秒" % (end - start)

[Python爬虫] 之十四:Selenium +phantomjs抓取媒介360数据的更多相关文章

  1. [Python爬虫] 之十:Selenium +phantomjs抓取活动行中会议活动

    一.介绍 本例子用Selenium +phantomjs爬取活动树(http://www.huodongshu.com/html/find_search.html?search_keyword=数字) ...

  2. [Python爬虫] 之九:Selenium +phantomjs抓取活动行中会议活动(单线程抓取)

    思路是这样的,给一系列关键字:互联网电视:智能电视:数字:影音:家庭娱乐:节目:视听:版权:数据等.在活动行网站搜索页(http://www.huodongxing.com/search?city=% ...

  3. [Python爬虫] 之十一:Selenium +phantomjs抓取活动行中会议活动信息

    一.介绍 本例子用Selenium +phantomjs爬取活动行(http://www.huodongxing.com/search?qs=数字&city=全国&pi=1)的资讯信息 ...

  4. [Python爬虫] 之十三:Selenium +phantomjs抓取活动树会议活动数据

    抓取活动树网站中会议活动数据(http://www.huodongshu.com/html/index.html) 具体的思路是[Python爬虫] 之十一中抓取活动行网站的类似,都是用多线程来抓取, ...

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

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

  6. PYTHON 爬虫笔记十:利用selenium+PyQuery实现淘宝美食数据搜集并保存至MongeDB(实战项目三)

    利用selenium+PyQuery实现淘宝美食数据搜集并保存至MongeDB 目标站点分析 淘宝页面信息很复杂的,含有各种请求参数和加密参数,如果直接请求或者分析Ajax请求的话会很繁琐.所以我们可 ...

  7. C#使用Selenium+PhantomJS抓取数据

    本文主要介绍了C#使用Selenium+PhantomJS抓取数据的方法步骤,具有很好的参考价值,下面跟着小编一起来看下吧 手头项目需要抓取一个用js渲染出来的网站中的数据.使用常用的httpclie ...

  8. selenium+PhantomJS 抓取淘宝搜索商品

    最近项目有些需求,抓取淘宝的搜索商品,抓取的品类还多.直接用selenium+PhantomJS 抓取淘宝搜索商品,快速完成. #-*- coding:utf-8 -*-__author__ =''i ...

  9. [Python爬虫] 之十二:Selenium +phantomjs抓取中的url编码问题

    最近在抓取活动树网站 (http://www.huodongshu.com/html/find.html) 上数据时发现,在用搜索框输入中文后,点击搜索,phantomjs抓取数据怎么也抓取不到,但是 ...

随机推荐

  1. Intersection of Two Linked Lists——经典问题

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 赤峰项目目前的mysql配置项目

    #BEGIN CONFIG INFO #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries #TYPE: SYSTEM ...

  3. 如何实现artTemplate模板的可重用性,以此框架打造自己的自定义组件

    问题的提出: 我们为什么非得要引入artTemplate来进行JSON数据的渲染呢? http://dagmom.iteye.com/blog/1671498http://bbs.csdn.net/t ...

  4. WordPress 性能优化:为什么我的博客比你的快

    WordPress 很慢? 很多博主都会感觉 WordPress 很慢?作为全世界最常用的建站和博客系统 WordPress 来说,在性能设计上肯定不会有太大的问题,WordPress 开发团队也肯定 ...

  5. 玩转树莓派 - 修改Raspbian软件源加快软件下载速度

    这是 meelo 原创的 玩转树莓派 系列文章 步骤1:登录到Raspbian的命令行界面 步骤2:修改Raspbian的软件源 软件源是Linux系统免费的应用程序安装仓库,很多的应用软件都会这收录 ...

  6. window 下 nginx+php+fastcgi 架设备忘

    1.配置Php.ini 1)extension_dir = "./ext" 修改这个路径为真实的php的ext路径 2);extension=php_mysql.dll ;exte ...

  7. WebDriver自动化测试工具(1)---环境搭建

    Webdriver是一个前端自动化测试工具,可以模拟用户点击链接,填写表单,点击按钮等操作,下面介绍其使用 一.下载WebdriverC#类库以及对应浏览器驱动 http://www.selenium ...

  8. ionic3包还原使用yarn命令执行步骤(解决ionic3懒加载报找不到 module的错误)

    使用cnpm 还原ionic3.6的依赖包的时候 可以正常还原,但是使用懒加载就会报找不到 module 的错误.最简单的解决办法是删除node_modules 挂个vpn 重新执行npm insta ...

  9. vue2.0--组件通信(非vuex法)

    写在前面: 1.父组件的data写法与子组件的data写法不同 //父组件 data:{ //对象形式 } //子组件 data:function(){ return { //函数形式 } } 2.引 ...

  10. ffmpeg测试程序

    ffmpeg在编译安装后在源码目录运行make fate可以编译并运行测试程序.如果提示找不到ffmpeg的库用LD_LIBRARY_PATH指定一下库安装的目录.