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

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

#!/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. JavaScript-闭包注意事项

    闭包允许内层函数引用父函数中的变量,但是该变量是最终值

  2. Git图文教程:从零到上传GitHub项目

    一:安装Git 从Git官网下载.安装客户端 二:本地建立代码仓库 在开始菜单中找到 Git Bash 并打开 配置身份 git config --global user.name "pen ...

  3. oracle数据字典-权限-角色

    每个数据库都提供了各自的数据字典的方案,虽然形式不同,但是目的和作用是一样的,比如在mysql里数据字典是在information_schema 里表现的,sqlserver则是在sys这个系统sch ...

  4. shell流程控制语句

    linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while,until),选择语句(case/select).下面我将通过例子介绍下,各个语句使用方法. 1 ...

  5. 通过命令行安装Android app

    手动安装安卓app的命令为:adb install -r C:\Users\Lihao\workspace\Appium_Demo\apps\app_F_1.3.0.apk

  6. python 字典访问的三种方法

    定义字典 dic = {'a':"hello",'b':"how",'c':"you"} 方法一: for key in dic: prin ...

  7. ASP.NET MVC 程序 报错“CS0012: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义”的解决办法

    运行MVC程序,具体报错信息如下: 解决方法: 打开Web.config在assemblies下加入<add assembly="System.Data.Entity, Version ...

  8. [mysql] ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.

    今天安装mysql遇到这样一个问题: ERROR 1862 (HY000): Your password has expired. To log in you must change it using ...

  9. [mysql] mysqldump 导出数据库表

    1.mysqldump的几种常用方法: (1)导出整个数据库(包括数据库中的数据) mysqldump -u username -p dbname > dbname.sql (2)导出数据库结构 ...

  10. IOS开发小项目—找色块游戏

    1.项目代码: @interface NextViewController () { int r ;//色块层数的全局变量 int m;//后面用于tag值的变化 UIView *view;//色块 ...