python 多线程抓取动态数据
利用多线程动态抓取数据,网上也有不少教程,但发现过于繁杂,就不能精简再精简?!
不多解释,直接上代码,基本上还是很好懂的。
#!/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 多线程抓取动态数据的更多相关文章
- Python 逆向抓取 APP 数据
今天继续给大伙分享一下 Python 爬虫的教程,这次主要涉及到的是关于某 APP 的逆向分析并抓取数据,关于 APP 的反爬会麻烦一些,比如 Android 端的代码写完一般会进行打包并混淆加密加固 ...
- scrapy-splash抓取动态数据例子一
目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...
- pythonのscrapy抓取网站数据
(1)安装Scrapy环境 步骤请参考:https://blog.csdn.net/c406495762/article/details/60156205 需要注意的是,安装的时候需要根据自己的pyt ...
- php外挂python脚本抓取ajax数据
之前我写过一遍php外挂python脚本处理视频的文章.今天和大家分享下php外挂python实现输入关键字搜索的脚本 首先我们先来分析一波网站: http://www.dzdpw.com/s.php ...
- scrapy-splash抓取动态数据例子八
一.介绍 本例子用scrapy-splash抓取界面网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- scrapy-splash抓取动态数据例子七
一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子六
一.介绍 本例子用scrapy-splash抓取中广互联网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子五
一.介绍 本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站 ...
- scrapy-splash抓取动态数据例子四
一.介绍 本例子用scrapy-splash抓取微众圈网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
随机推荐
- note: declarations in dependent base ‘std::basic_ios<char>’ are not found by unqualified lookup
错误信息如题所示. 修改前: template< typename _CharT, typename _Traits = std::char_traits<_CharT> > ...
- Linux-以指定用户运行redis
redis中无配置启动用户信息,需要添加redis用户,后以其启动 useradd -s /bash/false -M redis >& &
- 利用sql_trace跟踪一个指定会话的操作
1. sys用户给管理用户授权.SQL> grant execute on sys.dbms_system to andy;Grant succeeded.2. 查询被跟踪用户的sid,ser ...
- C# 二维list
public class ValueList : List<double> { public ValueList() { } } public ValueList[] valListArr ...
- Notepad++ Emmet安装方法教程
Notepad++ Emmet安装后出现 unknown exception提示插件无效Python Script Plugin did not accept the script.以下为记录解决方法 ...
- SQL 语句中的With(index())
SELECT m.Member_No, m.FirstName, m.Region_No FROM dbo.Member AS m WITH (INDEX (0)) 强制使用找到的第一个索引. 其 ...
- 透明度兼容性(ie8以上)
转载:http://www.cnblogs.com/PeunZhang/p/4089894.html demo代码:文件中,背景透明,文字不透明的研究和使用.zip
- 解决504 Gateway Time-out(nginx)
504 Gateway Time-out问题常见于使用nginx作为web server的服务器的网站 我遇到这个问题是在需要插入一万多条数据时候遇到的 一般看来, 这种情况可能是由于nginx默认的 ...
- TP控制器(Controller)
控制器的一些方法: Maincontroller.class.php文件: <?php namespace Home\Controller; use Think\Controller; clas ...
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...