Python爬上不得姐 并将段子写入数据库
#Python2.7 可以优化一下 前10页 每页点赞最多的段子 百思不得姐
# -*- coding: utf-8 -*-
import MySQLdb
import urllib,urllib2
import sys,re reload(sys)
sys.setdefaultencoding('utf-8') class TestBDJ():
def __init__(self):
pass
def getbdj(self):
lis = []
for h in range(1,10):
cc = []
tt = []
try:
url = 'http://www.budejie.com/text/'+str(h)
res = urllib.urlopen(url).read()
#print res
m = re.compile('<div class="j-r-list-c-desc">(.*?)</div>.*?<i class="icon-up ui-icon-up"></i> <span>(.*?)</span>',re.S) #当前版本可以这样匹配 没有匹配作者
tt = re.findall(m,res)
for i in tt:
cc.append(i[1]) #把每个段子点赞的数量放入list
aa = cc
aa = map(eval,aa) #需要将点赞的值由string转换成int
#print "befor %s",aa
for b in range(len(aa) - 1): #排序
for i in range(len(aa) - 1):
if aa[i] < aa[i + 1]:
aa[i], aa[i + 1] = aa[i + 1], aa[i]
#print "after %s",aa for t in range(5): #取每页前5条段子
duanzi = []
index = cc.index(str(aa[t])) #将点赞的数量转回string 根据下标 获取对应的段子
#print tt[index][0].replace("<br />","")
#print tt[index][1]
star = tt[index][1] #点赞人数
word = tt[index][0].replace("<br />","").encode('utf-8') #文字内容
duanzi.append(star)
duanzi.append(word)
lis.append(duanzi)
except Exception,e:
print e
return lis def load(self):
key = self.getbdj()
if key:
try:
conn = MySQLdb.connect(host='localhost', user='hehehe', passwd='****', db='tester', port=330,charset="utf8") # 加上charset="utf8" 指定编码格式 解决写入mysql中文乱码的问题
cur = conn.cursor()
conn.select_db('tester')
cur.execute('create table if not exists budejie(st int,info TEXT)')
for k in key:
cur.execute('insert into budejie values(%s,%s)',k)
cur.close()
conn.commit()
conn.close()
except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1]) return 0 if __name__=='__main__':
h = TestBDJ()
h.load()
Python 3 的需要稍微修改一下
#coding:utf-8
import urllib.request
import re
import importlib,sys for h in range(1,10):
cc = []
tt = []
try:
url = 'http://www.budejie.com/text/'+str(h)
res = urllib.request.urlopen(url).read()
res = res.decode("utf-8")
# print (res)
m = re.compile('<div class="j-r-list-c-desc">(.*?)</div>.*?<i class="icon-up ui-icon-up"></i> <span>(.*?)</span>',re.S) #当前版本可以这样匹配 没有匹配作者
tt = re.findall(m,res)
for i in tt:
cc.append(i[1]) #把每个段子点赞的数量放入list
aa = cc
aa = list(map(eval,aa)) #需要将点赞的值由string转换成int
#print "befor %s",aa
for b in range(len(aa)-1): #排序
for i in range(len(aa) - 1):
if aa[i] < aa[i + 1]:
aa[i], aa[i + 1] = aa[i + 1], aa[i]
#print "after %s",aa
for t in range(5): #取每页前5条段子
index = cc.index(str(aa[t])) #将点赞的数量转回string 根据下标 获取对应的段子
print (tt[index][0].replace("<br />",""))
print (tt[index][1])
except Exception as e:
print (e)
糗事百科,稍稍做了一下处理
# -*- coding: utf-8 -*-
import urllib,urllib2
import sys,re reload(sys)
sys.setdefaultencoding('utf-8') for h in range(1,8):
cc = []
tt = []
try:
url = 'http://www.qiushibaike.com/text/page/'+str(h)+'/?s=4891212'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36')
res = urllib2.urlopen(req).read()
#print res
m = re.compile('<div class="content">(.*?)</div>.*?<span class="stats-vote"><i class="number">(.*?)</i> 好笑</span>',re.S) #当前版本可以这样匹配 没有匹配作者 tt = re.findall(m,res)
for i in tt:
cc.append(i[1]) #把每个段子点赞的数量放入list
aa = cc
aa = map(eval,aa) #需要将点赞的值由string转换成int
#print "befor %s",aa
for b in range(len(aa) - 1): #排序
for i in range(len(aa) - 1):
if aa[i] < aa[i + 1]:
aa[i], aa[i + 1] = aa[i + 1], aa[i]
#print "after %s",aa
for t in range(5): #取每页前5条段子
index = cc.index(str(aa[t])) #将点赞的数量转回string 根据下标 获取对应的段子
print tt[index][0].replace("<br/>","")
print tt[index][1]
except Exception,e: print e
爬取含图片的段子:
# -*- coding: utf-8 -*-
import urllib,urllib2
import sys,re
reload(sys)
sys.setdefaultencoding('utf-8') for h in range(1,3):
cc = []
tt = []
try:
url = 'http://www.qiushibaike.com/imgrank/page/'+str(h)+'/?s=4891221'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36')
res = urllib2.urlopen(req).read()
#print res
m = re.compile('<div class="content">(.*?)</div>.*?target="_blank">.*?<img src=\"(.*?)\" alt=.*?<span class="stats-vote"><i class="number">(.*?)</i> 好笑</span>',re.S) #当前版本可以这样匹配 没有匹配作者
tt = re.findall(m,res)
for i in tt:
cc.append(i[2]) #把每个段子点赞的数量放入list
# print i[0]
# print i[1]
# print i[2]
aa = cc
aa = map(eval,aa) #需要将点赞的值由string转换成int
#print "befor %s",aa
for b in range(len(aa) - 1): #排序
for i in range(len(aa) - 1):
if aa[i] < aa[i + 1]:
aa[i], aa[i + 1] = aa[i + 1], aa[i]
#print "after %s",aa
for t in range(5): #取每页前5条段子
index = cc.index(str(aa[t])) #将点赞的数量转回string 根据下标 获取对应的段子
print index
print tt[index][0]
print tt[index][1]
print tt[index][2] except Exception,e:
print e
Python爬上不得姐 并将段子写入数据库的更多相关文章
- (python爬取小故事网并写入mysql)
前言: 这是一篇来自整理EVERNOTE的笔记所产生的小博客,实现功能主要为用广度优先算法爬取小故事网,爬满100个链接并写入mysql,虽然CS作为双学位已经修习了三年多了,但不仅理论知识一般,动手 ...
- python爬取糗事百科段子
初步爬取糗事百科第一页段子(发布人,发布内容,好笑数和评论数) #-*-coding:utf--*- import urllib import urllib2 import re page = url ...
- Python爬取拉勾网招聘信息并写入Excel
这个是我想爬取的链接:http://www.lagou.com/zhaopin/Python/?labelWords=label 页面显示如下: 在Chrome浏览器中审查元素,找到对应的链接: 然后 ...
- Python爬取新浪微博评论数据,写入csv文件中
因为新浪微博网页版爬虫比较困难,故采取用手机网页端爬取的方式 操作步骤如下: 1. 网页版登陆新浪微博 2.打开m.weibo.cn 3.查找自己感兴趣的话题,获取对应的数据接口链接 4.获取cook ...
- Python爬取全球是最大的电影数据库网站IMDb数据
在使用 Python 开发爬虫的过程中,requests 和 BeautifulSoup4(别名bs4) 应用的比较广泛,requests主要用于模拟浏览器的客户端请求,以获取服务器端响应,接收到的响 ...
- Python爬虫实战三之爬取嗅事百科段子
一.前言 俗话说,上班时间是公司的,下班了时间才是自己的.搞点事情,写个爬虫程序,每天定期爬取点段子,看着自己爬的段子,也是一种乐趣. 二.Python爬取嗅事百科段子 1.确定爬取的目标网页 首先我 ...
- 利用Python爬取豆瓣电影
目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...
- python 爬取段子网段子写入文件
import requests import re 进入网址 for i in range(1,5): page_url = requests.get(f"http://duanziwang ...
- 没有内涵段子可以刷了,利用Python爬取段友之家贴吧图片和小视频(含源码)
由于最新的视频整顿风波,内涵段子APP被迫关闭,广大段友无家可归,但是最近发现了一个"段友"的app,版本更新也挺快,正在号召广大段友回家,如下图,有兴趣的可以下载看看(ps:我不 ...
随机推荐
- onItemClick(AdapterView<?> parent, View view, int position, long id)
Public Methods public abstract void onItemClick (AdapterView<?> parent, View view, int positio ...
- Nand flash uboot 命令详解
转:http://blog.chinaunix.net/uid-14833587-id-76513.html nand info & nand device 显示flash的信息: DM365 ...
- jquery $(document).ready() 与js原生的window.onload的区别总结
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 ...
- C++ 如何得到当前进程所占用的内存呢?【转】
使用SDK的PSAPI (Process Status Helper)中的BOOL GetProcessMemoryInfo( HANDLE Process, PPROCESS_MEMORY_COUN ...
- STL 源码分析 (SGI版本, 侯捷著)
前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...
- Node.js 文件系统流pipe到Http响应流中
// 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); var fs=req ...
- 简单的图片处理servlet
好久没写博客了.近期做了一个比較有趣的商城项目,里面的业务还真的非常复杂,好在做了特殊的处理之后商城也能正常的使用了. 可是没中不足的就是图片目录和项目掺杂在一块,实在有些难以维护.之后找了点资料就搞 ...
- iOS uitableivewCell 下划线顶格
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...
- Linux——vi命令的使用
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi myfile 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command m ...
- oracle字符串处理相关
函数 返回 描述 例子 to_char(timestamp, text) text 把 timestamp 转换成 string to_char(timestamp 'now','HH12:MI:SS ...