Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ignore #467

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ weibo.db
*.log

.idea

run.sh

user_id_list.txt
50 changes: 50 additions & 0 deletions notice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding=utf-8
import smtplib
import time
# 需要 MIMEMultipart 类
from email.mime.multipart import MIMEMultipart
# 发送字符串的邮件
from email.mime.text import MIMEText

import update


def send(code, dest):
# 设置服务器所需信息
fromEmailAddr = '[email protected]' # 邮件发送方邮箱地址
password = 'wnumbsdltnkebehf' # 密码(部分邮箱为授权码)
# toEmailAddrs = ['[email protected]'] # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
toEmailAddrs = ['[email protected]'] # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发

msg = update.grid_notice(code, dest)
if '1' == msg:
return

# 设置email信息
# ---------------------------发送带附件邮件-----------------------------
# 邮件内容设置
message = MIMEMultipart()
# 邮件主题
message['Subject'] = '网格提醒-' + code
# 发送方信息
message['From'] = fromEmailAddr
# 接受方信息
message['To'] = toEmailAddrs[0]

# 邮件正文内容
message.attach(MIMEText(msg, 'plain', 'utf-8'))
# ---------------------------------------------------------------------

# 登录并发送邮件
try:
server = smtplib.SMTP('smtp.qq.com') # 邮箱服务器地址,端口默认为25
server.login(fromEmailAddr, password)
server.sendmail(fromEmailAddr, toEmailAddrs, message.as_string())
print('success')
server.quit()
except smtplib.SMTPException as e:
print("error:", e)


if __name__ == '__main__':
send('588000', 1.23)
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 -m weibo_spider
182 changes: 182 additions & 0 deletions update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# excel_u.py
# coding=utf-8
# 导入相应模块
import datetime
import re

import easyquotation
import matplotlib
import numpy as np
import openpyxl
import requests
from bs4 import BeautifulSoup

# 处理乱码
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family'] = 'sans-serif'
matplotlib.rcParams['axes.unicode_minus'] = False


# 查询股票信息
def get_stock(code):
quotation = easyquotation.use('qq') # 新浪 ['sina'] 腾讯 ['tencent', 'qq']
return quotation.real(code)[code] # 支持直接指定前缀,如 'sh000001'


# 每页最多50条数据
def get_html(code, start_date, end_date, page=1, per=10):
url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code={0}&page={1}&sdate={2}&edate={3}&per={4}'.format(
code, page, start_date, end_date, per)
rsp = requests.get(url)
html = rsp.text
return html


def get_fund(code, start_date, end_date, page=1, per=20):
# 获取html
html = get_html(code, start_date, end_date, page, per)
soup = BeautifulSoup(html, 'html.parser')
# 获取总页数
pattern = re.compile('pages:(.*),')
result = re.search(pattern, html).group(1)
total_page = int(result)
# 获取表头信息
heads = []
for head in soup.findAll("th"):
heads.append(head.contents[0])

# 数据存取列表
records = []
# 获取每一页的数据
current_page = 1
while current_page <= total_page:
html = get_html(code, start_date, end_date, current_page, per)
soup = BeautifulSoup(html, 'html.parser')
# 获取数据
for row in soup.findAll("tbody")[0].findAll("tr"):
row_records = []
for record in row.findAll('td'):
val = record.contents
# 处理空值
if not val:
row_records.append(np.nan)
else:
row_records.append(val[0])
# 记录数据
# print (row_records[0] , row_records[1])
records.append(row_records)
# 下一页
current_page = current_page + 1

return records


# 获取前五天的时间
def getLastDay():
today = datetime.date.today()
oneday = datetime.timedelta(days=5)
yesterday = today - oneday
return yesterday


# 获取今天的时间
def getToday():
return datetime.date.today()


# 场外基金
def run(code, row):
start_date = getLastDay()
end_date = getToday()

print(code, 'begin==========>')

records = get_fund(code, start_date, end_date)
# 最新的净值记录
record = records[0]
# print(record)
# 基金代码
# table.cell(row,1,code)
# 最新净值日期
net_date = record[0]
# 最新单位净值
net_value = record[1]

update_excel(net_date, net_value, row)
print(code, 'end==========>')


# 场内基金或股票
def run_stock(code, row):
print(code, 'begin==========>')
data = get_stock(code)
# print(data)
# 最新净值日期
# net_date = data['date']
dt = data['datetime']
# 格式化日期
net_date = dt.strftime('%Y-%m-%d')
# 最新单位净值
net_value = data['now']
update_excel(net_date, net_value, row)
print(code, 'end==========>')


# 更新Excel
def update_excel(net_date, net_value, row):
# 加载指定Excel
data = openpyxl.load_workbook('AssetAllocation.xlsx')
# 取第二张表
table = data['明细']
# 输出表名
# print(table.title)
table.cell(row, 11, net_date)
table.cell(row, 12, net_value)
data.save('AssetAllocation.xlsx')


def grid(code, row):
# 加载指定Excel
data = openpyxl.load_workbook('tt.xlsx')
# oad_workbook(file, read_only=True, data_only=True)
# 取第一张表
table = data.worksheets[0]
# 输出表名
print(table.title)
stock = get_stock(code)
net_value = stock['now']
print(net_value)
table.cell(row, 2, net_value)
data.save('tt.xlsx')


# 更新实验账户
def grid_1(code, cost, amount, dest):
stock = get_stock(code)
# print(stock)
open = stock['open']
now = stock['now']
name = stock['name']
profit = now - cost
rate = profit / cost
destRate = (dest - now) / now
print(name,
'成本价: {:.3f}'.format(cost),
'现价: {:.3f}'.format(now),
'涨跌幅: {:.2%}'.format(rate),
'盈利: {:.2f}'.format(profit * amount),
'目标价: {:.3f}'.format(dest),
'目标涨跌幅: {:.2%}'.format(destRate))
return profit * amount


def grid_notice(code, dest):
stock = get_stock(code)
# print(stock)
now = stock['now']
name = stock['name']
if now >= dest:
s = '叮咚!【' + name + '】达到目标价了,请及时操作哦~' + ' 现价: {:.3f}'.format(now)
return s
else:
return '1'
1 change: 1 addition & 0 deletions user_id_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2014433131 唐诗主任司马迁 2022-08-15 16:56
3 changes: 0 additions & 3 deletions weibo_spider/user_id_list.txt

This file was deleted.

22 changes: 11 additions & 11 deletions weibo_spider/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ def __init__(self):
self.original = None
self.video_url = ''

self.publish_place = ''
# self.publish_place = ''
self.publish_time = ''
self.publish_tool = ''
# self.publish_tool = ''

self.up_num = 0
self.retweet_num = 0
self.comment_num = 0
# self.up_num = 0
# self.retweet_num = 0
# self.comment_num = 0

def __str__(self):
"""打印一条微博"""
result = self.content + '\n'
result += u'微博发布位置:%s\n' % self.publish_place
# result += u'微博发布位置:%s\n' % self.publish_place
result += u'发布时间:%s\n' % self.publish_time
result += u'发布工具:%s\n' % self.publish_tool
result += u'点赞数:%d\n' % self.up_num
result += u'转发数:%d\n' % self.retweet_num
result += u'评论数:%d\n' % self.comment_num
result += u'url:https://weibo.cn/comment/%s\n' % self.id
# result += u'发布工具:%s\n' % self.publish_tool
# result += u'点赞数:%d\n' % self.up_num
# result += u'转发数:%d\n' % self.retweet_num
# result += u'评论数:%d\n' % self.comment_num
result += u'url:https://weibo.cn/comment/%s \n' % self.id
return result
62 changes: 52 additions & 10 deletions weibo_spider/writer/txt_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import logging
import smtplib
import sys
import time
# 需要 MIMEMultipart 类
from email.mime.multipart import MIMEMultipart
# 发送字符串的邮件
from email.mime.text import MIMEText

from .writer import Writer

Expand All @@ -19,9 +25,10 @@ def __init__(self, file_path, filter):
self.weibo_header = u'原创微博内容'
else:
self.weibo_header = u'微博内容'
self.weibo_desc = [('publish_place', '微博位置'), ('publish_time', '发布时间'),
('up_num', '点赞数'), ('retweet_num', '转发数'),
('comment_num', '评论数'), ('publish_tool', '发布工具')]
# self.weibo_desc = [('publish_place', '微博位置'), ('publish_time', '发布时间'),
# ('up_num', '点赞数'), ('retweet_num', '转发数'),
# ('comment_num', '评论数'), ('publish_tool', '发布工具')]
self.weibo_desc = [('publish_time', '发布时间')]

def write_user(self, user):
self.user = user
Expand All @@ -37,10 +44,10 @@ def write_user(self, user):
def write_weibo(self, weibo):
"""将爬取的信息写入txt文件"""

weibo_header = ''
if self.weibo_header:
weibo_header = self.weibo_header + ':\n'
self.weibo_header = ''
# weibo_header = ''
# if self.weibo_header:
# weibo_header = self.weibo_header + ':\n'
# self.weibo_header = ''

try:
temp_result = []
Expand All @@ -49,9 +56,44 @@ def write_weibo(self, weibo):
[v + ':' + str(w.__dict__[k])
for k, v in self.weibo_desc]))
result = '\n\n'.join(temp_result) + '\n\n'
# 同步到印象笔记
self.sendEmail(str(result))

with open(self.file_path, 'ab') as f:
f.write((weibo_header + result).encode(sys.stdout.encoding))
logger.info(u'%d条微博写入txt文件完毕,保存路径:%s', len(weibo), self.file_path)
# with open(self.file_path, 'ab') as f:
# f.write((weibo_header + result).encode(sys.stdout.encoding))
# logger.info(u'%d条微博写入txt文件完毕,保存路径:%s', len(weibo), self.file_path)
except Exception as e:
logger.exception(e)

def sendEmail(self, result):
# 设置服务器所需信息
fromEmailAddr = '[email protected]' # 邮件发送方邮箱地址
password = 'wnumbsdltnkebehf' # 密码(部分邮箱为授权码)
toEmailAddrs = ['[email protected]'] # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发

currentDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

# 设置email信息
# ---------------------------发送带附件邮件-----------------------------
# 邮件内容设置
message = MIMEMultipart()
# 邮件主题
message['Subject'] = currentDate
# 发送方信息
message['From'] = fromEmailAddr
# 接受方信息
message['To'] = toEmailAddrs[0]

# 邮件正文内容
message.attach(MIMEText(result, 'plain', 'utf-8'))
# ---------------------------------------------------------------------

# 登录并发送邮件
try:
server = smtplib.SMTP('smtp.qq.com') # 邮箱服务器地址,端口默认为25
server.login(fromEmailAddr, password)
server.sendmail(fromEmailAddr, toEmailAddrs, message.as_string())
print('success')
server.quit()
except smtplib.SMTPException as e:
print("error:", e)