#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>&nbsp;&nbsp;<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>&nbsp;&nbsp;<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爬上不得姐 并将段子写入数据库的更多相关文章

  1. (python爬取小故事网并写入mysql)

    前言: 这是一篇来自整理EVERNOTE的笔记所产生的小博客,实现功能主要为用广度优先算法爬取小故事网,爬满100个链接并写入mysql,虽然CS作为双学位已经修习了三年多了,但不仅理论知识一般,动手 ...

  2. python爬取糗事百科段子

    初步爬取糗事百科第一页段子(发布人,发布内容,好笑数和评论数) #-*-coding:utf--*- import urllib import urllib2 import re page = url ...

  3. Python爬取拉勾网招聘信息并写入Excel

    这个是我想爬取的链接:http://www.lagou.com/zhaopin/Python/?labelWords=label 页面显示如下: 在Chrome浏览器中审查元素,找到对应的链接: 然后 ...

  4. Python爬取新浪微博评论数据,写入csv文件中

    因为新浪微博网页版爬虫比较困难,故采取用手机网页端爬取的方式 操作步骤如下: 1. 网页版登陆新浪微博 2.打开m.weibo.cn 3.查找自己感兴趣的话题,获取对应的数据接口链接 4.获取cook ...

  5. Python爬取全球是最大的电影数据库网站IMDb数据

    在使用 Python 开发爬虫的过程中,requests 和 BeautifulSoup4(别名bs4) 应用的比较广泛,requests主要用于模拟浏览器的客户端请求,以获取服务器端响应,接收到的响 ...

  6. Python爬虫实战三之爬取嗅事百科段子

    一.前言 俗话说,上班时间是公司的,下班了时间才是自己的.搞点事情,写个爬虫程序,每天定期爬取点段子,看着自己爬的段子,也是一种乐趣. 二.Python爬取嗅事百科段子 1.确定爬取的目标网页 首先我 ...

  7. 利用Python爬取豆瓣电影

    目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...

  8. python 爬取段子网段子写入文件

    import requests import re 进入网址 for i in range(1,5): page_url = requests.get(f"http://duanziwang ...

  9. 没有内涵段子可以刷了,利用Python爬取段友之家贴吧图片和小视频(含源码)

    由于最新的视频整顿风波,内涵段子APP被迫关闭,广大段友无家可归,但是最近发现了一个"段友"的app,版本更新也挺快,正在号召广大段友回家,如下图,有兴趣的可以下载看看(ps:我不 ...

随机推荐

  1. Android之Activity的启动模式

    启动模式有4种,分别为:1.standard(默认)  -- 标准2.singleTop  -- 单顶3.singleTask -- 单任务4.singleInstance: -- 单例  -- 如果 ...

  2. Linux文件名小写的好处(转)

    说明:来自老阮的<为什么文件名要小写>的文章,其实我觉得应该说是<Linux文件名为什么要小写>会更合适些. 一.可移植性 Linux 系统是大小写敏感的,而 Windows ...

  3. Systems Performance: Enterprise and the Cloud 读书笔记系列

    http://blog.csdn.net/xiaonanAndroid/article/category/2557735

  4. 记录一次ceph recovery经历

    一次ceph recovery经历 背景 这是一个測试环境. 该环境中是cephfs 一共12个节点, 2个client.2个mds.8个osd mds: 2颗CPU,每一个4核.一共是8核. 128 ...

  5. mac 上python编译报错No module named MySQLdb

    mac 上python编译报错No module named MySQLdb You installed python You did brew install mysql You did expor ...

  6. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

  7. 渐进式 JPEG (Progressive JPEG)来提升用户体验

    1.概述 jpg格式分为:Baseline JPEG(标准型)和Progressive JPEG(渐进式).两种格式有相同尺寸以及图像数据,扩展名也是相同的,唯一的区别是二者显示的方式不同. Base ...

  8. 用二十秒记住几个PHP基础知识点

    数组: 索引数组:数组的键是整数的数组,从0開始. 关联数组:数组的键是字符串的数组 //索引数组 $arr=array('I','love','you'); //关联数组 $arr0=array(' ...

  9. 通过rinetd实现端口转发,同时访问阿里云RDS的内外网

    配置方法如下: 1 wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz& ...

  10. 点击tablecell中的一个按钮,确定cell所在的行

    - (void) del:(UIButton *) button { NSLog(@"%s",__FUNCTION__); UITableViewCell * cell = (UI ...