利用selenium 模拟浏览器打开页面,加载后抓取数据

#!/usr/bin/env python
# coding=utf-8 import urllib2
import re
from bs4 import BeautifulSoup
from selenium import webdriver
import time import sys
reload(sys)
sys.setdefaultencoding('utf-8') class BaikeSpider(): def __init__(self):
self.queue = ["http://baike.baidu.com/view/8095.htm",
"http://baike.baidu.com/view/2227.htm"]
self.base = "http://baike.baidu.com"
self.crawled = set()
self.crawled_word = set() # client = MongoClient("localhost",27017)
# self.db = client["baike_db"]["html"] def crawl(self):
browser = webdriver.Chrome()
cnt = 0
fw = open('./baike_keywords.txt','wb')
while self.queue:
url = self.queue.pop(0)
if url in self.crawled :
continue
self.crawled.add(url)
try:
browser.get(url)
res = {}
links = BeautifulSoup(urllib2.urlopen(url).read(),'lxml').find_all("a")
links = list(set(links))
for link in links:
if 'href' not in dict(link.attrs) or re.search(u"javascript",link['href']) or len(link['href'])<8:
continue
url = link['href']
if re.search(u"baike\.baidu\.com/view/\d+|baike\.baidu\.com/subview/\d+/\d+.htm",url) and url not in self.crawled:
self.queue.append(url)
elif re.match(u"view/\d+",url):
url = self.base+ url
if url not in self.crawled:
self.queue.append(url) cnt += 1
print cnt
if cnt % 10 == 0:
print 'queue',len(self.queue)
fw.close()
fw = open('./baike_keywords.txt','a+') res['url'] = url
res['title'] = browser.title.split(u"_")[0] if res['title'] in self.crawled_word:
print 'title',res['title'],'has crawled'
continue vote = browser.find_element_by_class_name("vote-count")
view = browser.find_element_by_id("j-lemmaStatistics-pv") res['voted'] = vote.text
res['viewed'] = view.text line = []
line.append(res['title'])
line.append(res['viewed'])
line.append(res['voted'])
line.append(res['url']) line = '\t'.join(line)
fw.write(line+'\n')
self.crawled_word.add(res["title"]) except Exception,e:
print e
continue if __name__=='__main__':
test = BaikeSpider()
test.crawl()

另外,使用chrome加载会比firefox快,且少报错,异常退出!

python抓取百度百科点赞数等动态数据的更多相关文章

  1. Python抓取百度百科数据

    前言 本文整理自慕课网<Python开发简单爬虫>,将会记录爬取百度百科"python"词条相关页面的整个过程. 抓取策略 确定目标:确定抓取哪个网站的哪些页面的哪部分 ...

  2. python3 - 通过BeautifulSoup 4抓取百度百科人物相关链接

    导入需要的模块 需要安装BeautifulSoup from urllib.request import urlopen, HTTPError, URLError from bs4 import Be ...

  3. Python爬虫之小试牛刀——使用Python抓取百度街景图像

    之前用.Net做过一些自动化爬虫程序,听大牛们说使用python来写爬虫更便捷,按捺不住抽空试了一把,使用Python抓取百度街景影像. 这两天,武汉迎来了一个德国总理默克尔这位大人物,又刷了一把武汉 ...

  4. Python——爬取百度百科关键词1000个相关网页

    Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...

  5. python抓取360百科踩过的坑!

    学习python一周,学着写了一个爬虫,用来抓取360百科的词条,在这个过程中.因为一个小小的修改,程序出现一些问题,又花了几天时间研究,问了各路高手,都没解决,终于还是自己攻克了,事实上就是对lis ...

  6. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  7. 使用python抓取百度搜索、百度新闻搜索的关键词个数

    由于实验的要求,需要统计一系列的字符串通过百度搜索得到的关键词个数,于是使用python写了一个相关的脚本. 在写这个脚本的过程中遇到了很多的问题,下面会一一道来. ps:我并没有系统地学习过pyth ...

  8. C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库

    第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...

  9. Python抓取百度汉字笔画的gif

    偶然发现百度汉语里面,有一笔一划的汉字顺序:          觉得这个动态的图片,等以后娃长大了,可以用这个教写字.然后就去找找常用汉字,现代汉语常用字表 .拿到这里面的汉字,做两个数组出来,一共是 ...

随机推荐

  1. Xcode 7安装KSImageNamed失败解决方法

    ## How do I use it? Build the KSImageNamed target in the Xcode project and the plug-in will automati ...

  2. PHP实现动态规划背包问题

    有一堆货物,有各种大小和价值不等的多个物品,而你只有固定大小的背包,拿走哪些能保证你的背包带走的价值最多 动态规划就是可以记录前一次递归过程中计算出的最大值,在之后的递归期间使用,以免重复计算. &l ...

  3. OpenJudge计算概论-排队游戏【这个用到了栈的思想】

    /*======================================================================== 排队游戏 总时间限制: 1000ms 内存限制: ...

  4. java中的包装类与装箱拆箱定义

    JAVA 中int类型转String类型的通常方法,有三种:  1.String.valueOf(int i)  2.Integer.toString(int i)  3.i+"" ...

  5. Zabbix的安装与部署---问题处理(php65.6.28 mysqli 报错误 处理)

    1)php65.6.28  mysqli  报错误 处理 http://www.xiaochengfu.com/index.php/index/detail/aid/92.html 2)linux下p ...

  6. Linux 环境下开机自启动Oracle服务

    使用如下命令查看Oracle相关服务是否已启动: ps aux | grep ora_ #若无ora_**_**相关的进程,则oracle数据库实例未启动 netstat -tlnup | grep ...

  7. Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解

    Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解 (本文转自: http://blog.csdn.net/yinhaide/article/details/44756 ...

  8. 【linux】linux服务管理

    ps:xinetd只要求了解

  9. (转)C# wnform 请求http ( get , post 两种方式 )

    本文转载自:http://www.cnblogs.com/hailexuexi/archive/2011/03/04/1970926.html 1.Get请求 string strURL = &quo ...

  10. 【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

    架构简介 前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构.此架构主要是由kee ...