正经Python汤不热爬虫
转自:https://github.com/facert/tumblr_spider
install
pip install -r requirements.txt
run
python tumblr.py username (usename 为任意一个热门博主的 usename)
snapshoot
爬取结果
user.txt
是爬取的博主用户名结果,source.txt
是视频地址集
原理
根据一个热门博主的 usename, 脚本自动会获取博主转过文章的其他博主的 username,并放入爬取队列中,递归爬取。
申明
这是一个正经的爬虫(严肃脸),爬取的资源跟你第一个填入的 username 有很大关系,另外由于某些原因,导致 tumblr 被墙,所以最简单的方式就是用国外 vps 去跑。
# -*- coding:utf-8 -*-
import signal
import sys
import requests
import threading
import queue
import time
from bs4 import BeautifulSoup mutex = threading.Lock()
is_exit = False class Tumblr(threading.Thread): def __init__(self, queue):
self.user_queue = queue
self.total_user = []
self.total_url = []
self.f_user = open('user.txt', 'a+')
self.f_source = open('source.txt', 'a+') threading.Thread.__init__(self) def download(self, url):
res = requests.get(url) source_list = []
soup = BeautifulSoup(res.text)
iframes = soup.find_all('iframe')
tmp_source = []
for i in iframes:
source = i.get('src', '').strip()
if source and source.find('https://www.tumblr.com/video') != -1 and source not in self.total_url:
source_list.append(source)
tmp_source.append(source)
print (u'新增链接:' + source) tmp_user = []
new_users = soup.find_all(class_='reblog-link')
for user in new_users:
username = user.text.strip()
if username and username not in self.total_user:
self.user_queue.put(username)
self.total_user.append(username)
tmp_user.append(username)
print (u'新增用户:' + username) mutex.acquire()
if tmp_user:
self.f_user.write('\n'.join(tmp_user)+'\n')
if tmp_source:
self.f_source.write('\n'.join(tmp_source)+'\n')
mutex.release() def run(self):
global is_exit
while not is_exit:
user = self.user_queue.get()
url = 'http://%s.tumblr.com/' % user
self.download(url)
time.sleep(2)
self.f_user.close()
self.f_source.close() def handler(signum, frame):
global is_exit
is_exit = True
print ("receive a signal %d, is_exit = %d" % (signum, is_exit))
sys.exit(0) def main(): if len(sys.argv) < 2:
print ('usage: python tumblr.py username')
sys.exit()
username = sys.argv[1] NUM_WORKERS = 10
q = queue.Queue()
# 修改这里的 username
q.put(username) signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler) threads = []
for i in range(NUM_WORKERS):
tumblr = Tumblr(q)
tumblr.setDaemon(True)
tumblr.start()
threads.append(tumblr) while True:
for i in threads:
if not i.isAlive():
break
time.sleep(1) if __name__ == '__main__':
main()
正经Python汤不热爬虫的更多相关文章
- Python初学者之网络爬虫(二)
声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途.转载请附上此文章地址 本篇文章Python初学者之网络爬虫的继续,最新代码已提交到https://github.com/octans ...
- 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程
点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...
- 【Python】:简单爬虫作业
使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...
- 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道 使用python/casperjs编写终极爬虫-客户端App的抓取
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
前面一直强调Python运用到网络爬虫方面很有效,这篇文章也是结合学习的Python视频知识及我研究生数据挖掘方向的知识.从而简介下Python是怎样爬去网络数据的,文章知识很easy ...
- 洗礼灵魂,修炼python(69)--爬虫篇—番外篇之feedparser模块
feedparser模块 1.简介 feedparser是一个Python的Feed解析库,可以处理RSS ,CDF,Atom .使用它我们可从任何 RSS 或 Atom 订阅源得到标题.链接和文章的 ...
- 洗礼灵魂,修炼python(50)--爬虫篇—基础认识
爬虫 1.什么是爬虫 爬虫就是昆虫一类的其中一个爬行物种,擅长爬行. 哈哈,开玩笑,在编程里,爬虫其实全名叫网络爬虫,网络爬虫,又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者 ...
- 使用Python + Selenium打造浏览器爬虫
Selenium 是一款强大的基于浏览器的开源自动化测试工具,最初由 Jason Huggins 于 2004 年在 ThoughtWorks 发起,它提供了一套简单易用的 API,模拟浏览器的各种操 ...
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
随机推荐
- Kubernetes集群详细介绍及部署
kubernetes的介绍: kubernetes是谷歌在2014年6月的一个开源集群项目,使用go语言开发,因为除了ks后只有8个字母又被称为k8s. k8s的主要作用: 自动化部署 扩展容器的管理 ...
- python代码优化-----cpu和内存监控
1.memory_profiler可以监控代码的内存消耗及增长量,以下面的代码为例. 发现在for循环里增加了0.3MB,这个工具可以帮助我们定位内存泄露的问题. 2.profile与cProfile ...
- (转) oracle清空数据库脚本
在开发过程中,可能经常需要重新初始化数据库,在初始化之前,我们肯定希望不再有以前的老表.存储过程等用户对象,用下面的教本就可以做到这一点: BEGIN FOR rec IN (SELE ...
- 一个不错的vue项目
项目演示: https://www.xiaohuochai.cc 项目地址:https://github.com/littlematch0123/blog-client
- MySQL数据表
创建数据表 CREATE TABLE IF NOT EXISTS ([列名column][类型type][约束可选]) 查看数据表结构 DESC <表名> 修改数据表结构 ALTER ...
- 【leetcode】1221. Split a String in Balanced Strings
题目如下: Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced ...
- opengl中相关的计算机图形变换矩阵之:齐次坐标 (摘编)
模型视图变换(几何变换)矩阵: 1. 齐次坐标:两条平行线也可以相交. 在欧几里得空间中,两条平行线是无法相交的,但是在投影空间(Projective Space)这条定理就不再适用了. 比如上图中, ...
- jvisualvm性能监控
一.配置JMX 1.进入tomcat bin目录 vim catalina.sh #!/bin/sh下面加入: #!/bin/sh JAVA_OPTS="-Dcom.sun.manageme ...
- esLint——规范你的代码(转)
团队协作时,若是团队的代码风格统一,能够大大减少沟通成本. 什么是 ESLint ? ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码 ...
- XAMPP 1.8.2-2 Apache Web Server won't start, always stops immediately
sudo apachectl stop apachectl是Apache超文本传输协议服务器的前端程序. 其设计意图是帮助管理员控制Apachehttpd后台的功能. MacOS中安装完Apache之 ...