利用多线程动态抓取数据,网上也有不少教程,但发现过于繁杂,就不能精简再精简?!

不多解释,直接上代码,基本上还是很好懂的。

#!/usr/bin/env python
# coding=utf-8 import urllib2
import re,sys
from bs4 import BeautifulSoup
from selenium import webdriver
import threading
import time
reload(sys)
sys.setdefaultencoding("utf-8") queue = [
"http://baike.baidu.com/view/8332.htm",
"http://baike.baidu.com/view/145819.htm",
"http://baike.baidu.com/view/643415.htm",
"http://baike.baidu.com/view/157424.htm",
"http://baike.baidu.com/view/149759.htm",] crawled_url = set()
crawled_word = set() cnt = 0 class BaikeSpider(threading.Thread):
"""
模拟浏览器打开页面,多线程爬取数据
""" def __init__(self,name):
threading.Thread.__init__(self)
self.name = str(name) self.browser = webdriver.Chrome()
# 将抓取数据写入各自的文件
self.fw = open("baike_words_"+self.name+".txt","wb") def run(self):
global queue
global crawled_url
global crawled_word
global cnt while queue:
url = queue.pop(0) try:
self.browser.get(url)
# 休眠0.5s,等待数据加载
time.sleep(0.5)
links = BeautifulSoup(urllib2.urlopen(url).read(),"lxml").find_all("a")
vote = self.browser.find_element_by_class_name("vote-count").text
view = self.browser.find_element_by_id("j-lemmaStatistics-pv").text
word = self.browser.title.split(u"_")[0] if word in crawled_word or url in crawled_url:
continue
else:
for link in links:
if 'href' not in dict(link.attrs) or re.search(u"javascript",link['href']) or len(link['href'])
<8:
continue
tmpurl = link["href"]
if re.search("baike.baidu.com/view/\d+|baike.baidu.com/subview/\d+(/\d+)?",tmpurl) and tmpurl n
ot in crawled_url:
queue.append(tmpurl) crawled_url.add(url)
linedata = word+"\t"+view+"\t"+vote+"\t"+url+"\n"
self.fw.write(linedata) except Exception,e:
print 'error',e
continue cnt += 1
print cnt,self.name,'len',len(queue) def __exit__(self):
self.fw.close() if __name__=='__main__':
"""
开5个线程
"""
for i in range(5):
t = BaikeSpider(i)
t.start()

python 多线程抓取动态数据的更多相关文章

  1. Python 逆向抓取 APP 数据

    今天继续给大伙分享一下 Python 爬虫的教程,这次主要涉及到的是关于某 APP 的逆向分析并抓取数据,关于 APP 的反爬会麻烦一些,比如 Android 端的代码写完一般会进行打包并混淆加密加固 ...

  2. scrapy-splash抓取动态数据例子一

    目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...

  3. pythonのscrapy抓取网站数据

    (1)安装Scrapy环境 步骤请参考:https://blog.csdn.net/c406495762/article/details/60156205 需要注意的是,安装的时候需要根据自己的pyt ...

  4. php外挂python脚本抓取ajax数据

    之前我写过一遍php外挂python脚本处理视频的文章.今天和大家分享下php外挂python实现输入关键字搜索的脚本 首先我们先来分析一波网站: http://www.dzdpw.com/s.php ...

  5. scrapy-splash抓取动态数据例子八

    一.介绍 本例子用scrapy-splash抓取界面网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...

  6. scrapy-splash抓取动态数据例子七

    一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  7. scrapy-splash抓取动态数据例子六

    一.介绍 本例子用scrapy-splash抓取中广互联网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  8. scrapy-splash抓取动态数据例子五

    一.介绍 本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站 ...

  9. scrapy-splash抓取动态数据例子四

    一.介绍 本例子用scrapy-splash抓取微众圈网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...

随机推荐

  1. jquery下ie的margin-left ----bug 以及parseInt方法bug

    ie下使用jquery的方法css('margin-left')可能会出现'auto'----从而使结果不可计算,即便使用parseInt()方法也不行 因为parseInt()方法的bug是如果参数 ...

  2. JNI学习2:android 调用C语言方法与C语言调用android方法

    #include <jni.h> #include <stdio.h> #include <stdlib.h> #include <jni.h> #in ...

  3. Python字典笔记

    1.字典是Python中=唯一的映射类型.映射类型对象里哈希值(键,key)和只指向的对象(值,value)是一对多的关系,一个字典是可变的,是一个容器类型.字典类型和序列类型(元组,列表)的不同在于 ...

  4. python tornado框架使用

    处理方法 t_handler.py from tornado.web import RequestHandler class IndexHandler(RequestHandler): def get ...

  5. python sys.argv[]

    sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...

  6. 移动端H5页面的设计稿尺寸大小规范-转载自http://www.chinaz.com/design/2015/1103/465670.shtml

    机屏幕尺寸,设计稿应该按照哪一个尺寸作为标准尺寸.现在已经有2K分辨率的手机屏幕了,设计稿是不是也要把宽高跟着最大分辨率来设计.显然不是. 请注意:(以下所有讨论内容和规范均将viewport设定为c ...

  7. DW(五):polybase集群安装

    目录: Prerequisites 集群配置规划 polybase install firewall config 集群配置 删除计算节点 install Prerequisites Microsof ...

  8. Neutron分析(2)——neutron-server启动过程分析

    neutron-server启动过程分析 1. /etc/init.d/neutron-server DAEMON=/usr/bin/neutron-server DAEMON_ARGS=" ...

  9. input 字符限制

    1,文本框只能输入数字代码(小数点也不能输入): onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste=&q ...

  10. visibility和display的区别

    大多数人很容易将CSS属性display和visibility混淆,它们看似没有什么不同,其实它们的差别却是很大的. visibility属性用来确定元素是显示还是隐藏的,这用visibility=& ...