-
从贴吧热门贴的源代码获取发帖人,发帖时间,发帖内容,并存入csv文件中
2019-11-20 21:16:32# -*- coding: utf-8 -*- import re import csv name_list = [] f = open("result.csv", "w+", encoding="utf-8") ...csv_writer.writerow(["用户名", "发帖时间", "发帖内容"]) file = open("s...# -*- coding: utf-8 -*- import re import csv name_list = [] f = open("result.csv", "w+", encoding="utf-8") csv_writer = csv.writer(f) csv_writer.writerow(["用户名", "发帖时间", "发帖内容"]) file = open("source.txt", "r") #源代码存储文件 content = file.read() file.close() #匹配发帖人 pattern1 = re.compile(r'''p_author_name.*?>(.*)''', re.M | re.I) pattern2 = re.compile(r"<img.*?/>") name = pattern1.findall(content) for str1 in name: str1 = re.sub(pattern2, "", str1) str1 = re.sub(r"</a>", "", str1) name_list.append(str1) #匹配发帖时间 pattern3 = re.compile(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}") time_list = pattern3.findall(content) #匹配发帖内容 pattern4 = re.compile(r'''post_content_\d{12}.*?>\s*(.*?<)''', re.M | re.I) info = pattern4.findall(content) info_list = [] for str1 in info: str1 = re.sub(r"<", "", str1) info_list.append(str1) #zip()内置函数同时遍历三个列表 for name, time, info in zip(name_list, time_list, info_list): csv_writer.writerow([name, time, info])
-
协程爬取贴吧里发帖内容(redis做任务队列,mongo存储)
2018-08-28 01:13:54# 当内容不为空时,将内容存到mongo里 my_set.save({ 'content' :content}) #print(content) # 将爬取过的任务放到redis的history集合里,也就是已完成任务队列 r.sadd( 'history' , url) t1 = time.time()...是用redis做任务队列时,要思考:
- 用什么数据类型来做任务队列
- 怎样才能防止重复爬取
首先了解一下redis可以存储什么数据类型:
- 字符串String
- 哈希hash
- 列表list
- 集合set
- 有序集合zset
浏览完这几种数据类型的功能之后,决定用list来做任务队列,用set来解决思考的问题,就是防止重复爬取的问题。
大概思路:- 使用list当作未完成任务队列,存储还没有爬的url
- 使用set当作已完成任务队列,存储已经爬取的url
- 每次爬虫程序从list未完成任务队列获取任务的时候,都去set已完成任务队列里面验证一下,如果已完成队列里已经有了,就舍弃掉,如果没有,就开始爬取,并将这个url加入到已爬取的任务队列
这样做的方便之处在于:每当我往list未完成任务队列里加任务的时候,我不用考虑这个任务有没有爬过,这个任务是不是已经在未爬取任务队列了,我只需要往里加就行了,当爬虫去取的时候,让爬虫程序去做这个操作。
以下是具体代码
算是一个生产消费把,master往队列里塞任务,parser使用get_html的返回值进行解析,然后入库import requests from lxml import etree import redis import asyncio,aiohttp import pymongo conn = pymongo.MongoClient('localhost',27017) db = conn.nicedb # 指定数据库名称,连接nicedb数据库,没有则自动创建 my_set = db.test_set # 使用test_set集合,没有则自动创建 # 以上两步都是延时操作,当往数据库插入第一条数据的时候,才会真正的创建数据库和集合 # decode_responses=True,记得加这个参数,不加的话取出来的数据都是bytes类型的 r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 2,decode_responses=True) # pool = redis.ConnectionPool(host = '127.0.0.1', port = 6379, db = 2) # r = redis.StrictRedis(connection_pool=pool,decode_responses=True) def master(page): url = 'https://tieba.baidu.com/f?kw=美女&ie=utf-8&pn={}'.format(page*50) base = 'https://tieba.baidu.com' res = requests.get(url).text html = etree.HTML(res) half_urls = html.xpath("//div[@class='threadlist_title pull_left j_th_tit ']/a/@href") full_urls = [base + i for i in half_urls] for url in full_urls: # 从url_list列表头部塞任务,也就是url r.lpush('url_list',url) #print(r.llen('url_list')) async def get_html(url): async with asyncio.Semaphore(5): # 限制并发数为5个 async with aiohttp.ClientSession() as session: async with session.get(url) as html: # errors='ignore',不加这个参数的话,会报错,具体错误内容见下面图片 response = await html.text(encoding='utf-8',errors='ignore') return response async def parse(): while True: # 从redis的url_list列表取任务,从右边开始取 url = r.rpop('url_list') if url == None: break # 判断这个任务是否已经做过了,也就是判断这个url在没在redis的history集合里 if r.sismember('history',url) == 1: continue response = await get_html(url) html = etree.HTML(response) content = html.xpath("//div[@class='left_section']/div[2]/div[1]//cc/div[1]/text()")[0].strip() if content != '': # 当内容不为空时,将内容存到mongo里 my_set.save({'content':content}) #print(content) # 将爬取过的任务放到redis的history集合里,也就是已完成任务队列 r.sadd('history', url) t1 = time.time() # 爬取前10页 for i in range(10): master() # async的一些步骤 loop = asyncio.get_event_loop() tasks = [parse() for _ in range(15)] loop.run_until_complete(asyncio.wait(tasks)) loop.close() t2 = time.time() print(t2-t1) # 最后用时:32.930299043655396 # 把mongo数据库换成mysql后,用时:43.06192493438721
这是代码注释中提到的,编码错误
-
如何过滤发帖内容...求大神指教...
2013-11-14 10:47:13jsp界面editor去掉不和谐的内容或图片... 最好是使用js验证的... 求指教设计思路或核心算法... 谢谢 -
Discuz!发帖预留内容
2013-07-15 16:23:26发帖预留内容 这个是看到站长网(ADMIN5)论坛用到了,那天请教了一下图王,了解了修改方法,今天分享出来. 演示如下, 修改方法: 打开模板文件templates\default\hempost_new...Discuz!发帖预留内容
这个是看到站长网(ADMIN5)论坛用到了,那天请教了一下图王,了解了修改方法,今天分享出来.
演示如下,
修改方法:
打开模板文件templates\default\hempost_newthread.htm
查找
替换为
注意:
-
我想在discuz发帖页面内容区域中添加内容
2020-10-12 12:38:14dz原始的 看下截图吧 就是这个位置 [img=https://img-bbs.csdn.net/upload/202010/12/1602477453_303266.png][/img] 我想用js或者jquery像他植入指定的内容 -
花牙百度贴吧发帖辅助2.1中文免费绿色版百度贴吧自动发帖机
2019-07-31 08:06:42用户自定义发帖内容,同时可以添加各种随机变量,以防止被屏蔽。软件完全免费,是你混迹贴吧,营销推广的利器。 花牙百度贴吧发帖辅助功能特点 1、 软件完全免费。 2、 操作超级简单、只需导入账号、导入贴吧名、... -
python 贴吧发帖数_搜索贴吧内容,摘取其标题、发帖人、发帖时间、评论数,并保存到数据库...
2020-12-04 05:38:44[python]代码库'''针对贴吧前5页(可改)实现功能:1、保存所查询的网页内容到文件2、摘取每个帖子的属性信息(标题,发帖人,发帖时间,评论数),并保存到数据库中3.根据标题从数据库中搜索帖子'''from urllib.request...[python]代码库'''
针对贴吧前5页(可改)实现功能:
1、保存所查询的网页内容到文件
2、摘取每个帖子的属性信息(标题,发帖人,发帖时间,评论数),并保存到数据库中
3.根据标题从数据库中搜索帖子
'''
from urllib.request import urlopen
from urllib.parse import urlencode
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import sqlite3
import os
key = input('请输入一个查询关键字')
#key = '芙蓉'
args = {
'kw': key,
'ie': 'utf-8'
}
url1 = 'http://tieba.baidu.com/f?' + urlencode(args)
def get_one_page(index):
url = url1 + '&pn={}'.format(index * 50)
response = urlopen(url)
return response.read().decode()
def save_one_page(index, html):
filename = 'tieba\\tieba_{}.html'.format(index + 1)
with open(filename, 'w', encoding='utf-8') as file:
file.write(html)
pass
db_file = 'tieba.db'
def create_table():
conn = sqlite3.connect(db_file)#1连接数据库
cursor = conn.cursor()#2创建执行对象
cursor.execute('''
create table tieba(
id integer primary key autoincrement ,
title text,
author text,
time text,
num int
)
''')#3
conn.commit()#4.提交操作,对于可以修改数据库内容的语句必须提交
conn.close()#5.关闭连接
def save(tieba):
#连接
conn = sqlite3.connect(db_file)
#创建执行对象
cursor = conn.cursor()
#执行SQL语句
cursor.execute('''
insert into tieba
(title,author,time,num)
values
(?, ?, ?, ?)
''',(tieba.get('title'),tieba.get('author'),tieba.get('time'),
tieba.get('num')))
#提交
conn.commit()
#关闭
conn.close()
# 根据标题关键字查询数据库
def find_by_title(key):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
result = cursor.execute('''
select * from tieba
where title like ?
''', ('%'+key+'%',))
#查询不需要提交
ls = []
for row in result:
movie = {}
movie['id'] = row[0]
movie['title'] = row[1]
movie['auther'] = row[2]
movie['time'] = row[3]
movie['num'] = row[4]
ls.append(movie)
conn.close()
return ls
def get_tieba_info(html):
soup = BeautifulSoup(html, 'html.parser')
ls_con = soup.select('#thread_list li')
print(len(ls_con))
ls = []#定义一个空列表,用来存放贴吧的信息
for con in ls_con:
tieba = {}
a = con.find('a', attrs={"class": 'j_th_tit'})
# print(a)
if a == None:
continue
else:
title = a.get('title')
# print(title)
tieba['title'] = title
p = con.find('span', attrs={'class': 'tb_icon_author'})
author = p.get('title')
# print(author)
author = author.replace('\n', '') # 去掉字符串结尾的\n
author = author.replace('主题作者:', '') # 去掉字符串中的多余字符
# print(author)
tieba['author'] = author
p = con.find('span', attrs={'class': 'pull-right'})
# print(p)
time = p.get_text()
# print(time)
tieba['time'] = time
p = con.find('span', attrs={'class': 'threadlist_rep_num'})
# print(p)
num = p.get_text()
# print(num)
tieba['num'] = num
ls.append(tieba)
return ls
if __name__ == '__main__':
if not os.path.exists(db_file):#若已经存在就不再创建新表
create_table()
tieba_list = []
'''
#仅保存第一页的代码
html = get_one_page(0)
get_tieba_info(html)
tieba_list += get_tieba_info(html)
print(tieba_list)
'''
#保存到数据库,最后要测试
for index in range(0, 5):
html = get_one_page(index)
tieba_list += get_tieba_info(html)
# 保存网页到文件
save_one_page(index, html)
#把数据保存到表中
for t in tieba_list:
save(t)
key = input('请输入一个关键词')
ls = find_by_title(key)
for t in ls:
print(t)
[代码运行效果截图]
-
SQL个人笔记——社区内容发帖数据统计逻辑总结
2020-11-06 21:30:38====================帖子内容相关数据=========================== --1-10月发帖量和发帖人数统计 select trunc(a.day,'MM') as month ,count(distinct a.tid) as tid_num ,count(distinct a.openid) as uv ,... -
discuz 发帖, 无内容修复
2014-03-02 18:28:00Bug原因: X3的发帖表单中没有parseurloff这个可选框, 所以,出错,导致发帖的表单form.message不能赋值, 修复方案即为: 注释掉form_post.js中的72,73,74行。 转载于:... -
小程序评论回复和发帖违规内容、图片检测功能实战
2020-02-05 19:35:30这次分享下“发帖功能”,这个功能其实风险蛮大的,特别是对一些敏感言论的控制,如果没有做好可能导致小程序被封,所以除了必要的人工审核和巡查以外,我们需要一些微信安全监测API的帮忙,在AI加持下,现在很多大... -
SemiAutomatic:利用正则表达式,从百度贴吧源代码的文本中,提取出每一层的发帖人,发帖时间和帖子内容-...
2021-03-24 05:00:16Spider-baidu-SemiAutomatic:利用正则表达式,从百度贴吧源代码的文本中,提取出每一层的发帖人,发帖时间和帖子内容 -
百度贴吧发帖软件_百度贴吧发帖顶贴快速引流推广
2020-12-02 19:56:26目前贴吧引流还是非常好做的,相对其他引流也比较简单。贴吧是百度旗下的产品,位高权重,也作为全球最大的中文社区,流量非常可观,所以百度...1、发帖内容不能出现违规词、违禁词,这里提供一些敏感词,大家可以... -
发帖
2019-02-26 21:22:36string postData欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右... -
社区类 App 如何引导用户发帖和产生内容?
2019-10-08 21:17:01作者:Pmer在路上链接:http://www.zhihu.com/question/25502904/answer/31342246来源:知乎著作权归作者所有,转载请联系作者获得授权。ugc的产出,在强关系社交和弱关系...增强内容的传达效果,通过诸如“特别关... -
【原创】获取网页中所有密码和发帖标题内容
2015-05-26 15:55:41通过微软的IE接口获取到所有IE浏览器中的密码,帖子标题和内容 -
jq实现简易发帖子,包含头像,名字,内容等
2020-03-06 21:03:05一.html <div class="bbs-box"> <div> <span>发帖人</span> <input type="text" name="user-name" > </div> <div> <span>帖子类型</spa... -
股吧发帖机 东方财富网股吧发帖机 v1.0
2020-10-17 05:46:24东方财富网股吧发帖机是一款金融股票宣传的好帮手,用于解放双手发帖带来的繁琐,提升工作效率和工作业绩。东方财富网股吧发帖机软件使用:1. 配置好发帖标题 内容,准备好发帖 -
如何查看论坛java隐藏内容_DZ论坛如何去掉“今日”“昨日”发帖数显示
2021-03-18 09:13:19DZ论坛开始建设不久,论坛没什么人气,每天的发帖数很少,甚至经常为零,那么如何去掉论坛首页左上方显示的“今日”发帖数、“昨日”发帖数呢?通过FlashFXP等工具或直接进入论坛空间的根目录,依次打开template/...