电影天堂每日抓取首页最新电影

python每日抓取电影天堂首页最新电影推送到微信

import requests
from lxml import etree

class Movie(object):
  def __init__(self):
    self.url = 'https://www.dytt8.net/html/gndy/dyzz/index.html'
    self.headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"}
    self.movies = []
    self.html = ''

  def get_all(self):
    movies = ''
    r = requests.get(url=self.url, headers=self.headers)
    r.encoding = 'gb2312'
    # print(r.text)
    tree = etree.HTML(r.text)
    tables = tree.xpath('//div[@class="co_content8"]/ul/td/table')
    # print(tables)
    for table in tables:
      title = table.xpath('./tr[2]/td[2]/b/a/text()')[0]
      htmls = table.xpath('./tr[2]/td[2]/b/a/@href')[0]
      html = 'https://www.dytt8.net' + htmls
      # print(title)
      self.movies.append(title + '\n' + html + '\n')
    self.html = "".join(self.movies)
    return self.html
dy = Movie()
# print(dy.get_all())
dy.get_all()

Bilibili视频下载批量重命名

软件是自己写给朋友用的,发布的时候博客还没恢复,直接发到吾爱论坛了,再给自己博客发一个。

用Python写的,UI是PySimpleGui,打包成exe很大。

首先要用唧唧DOWN下载,其他没有测试,由于用正则匹配写的,所以其他软件下载的文件名称可能不匹配。
唧唧DOWN,自己搜索,哪都有。
下载时候不要勾选自动新建文件夹
下载

选择目录,直接转换就行了。。
成品

下载地址:
https://modys.lanzout.com/iTJ6l09a8ikb 密码:3hpv

安装python3出现checking how to run the C preprocessor... /lib/cpp

出现checking how to run the C preprocessor... /lib/cpp configure: error: C preprocessor "/lib/cpp" fails sanity check
是由于没有安装kernel-headers
安装yum install kernel-headers时如果出现Plugin "product-id" can't be imported Loaded plugins: fastestmirror, langpac
禁用加速插件,修改/etc/yum/pluginconf.d/fastestmirror.conf
将enabled = 1//由1改为0,禁用该插件
修改yum.conf
把exclude= 的内容注释掉即可

企业微信定时推送到普通微信,每日提醒工作

前几个月写了一个自动推送的文章,由于作者接口出现问题不能使用,大概是上周四出现问题,一直到现在还不能使用。可能是提醒习惯了,我每天的工作日报有时候就忘记写。想起来调用server酱,虽然免费5条够用,但是折叠起来,看起来很麻烦,通过搜索,找到一篇文章,不需要部署服务,直接通过企业微信接口调用。
接口原作者:https://ley.best/push-msgs-to-wechat-and-dingding/
原作者的文章,用的是图文消息,但推送的字体很小,而且不能超过512字节,发送的消息文字显示不全;文本消息可以达到2048字节,所以改成文本。
自己想要什么都可以更改,看企业微信接口文档:https://work.weixin.qq.com/api/doc/90000/90135/90236
everyday

效果图如下:
wechat

from lxml import etree
from datetime import datetime
import requests, sys, urllib, json


class Worker:
    def __init__(self):
        self.now_time = datetime.today()
        self.headers= {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
        self.url = 'http://api.tianapi.com/txapi/jiejiari/index?key=自己的&date='
    def my_job(self):
        url = self.url + str(self.now_time)
        r = requests.get(url=url, headers=self.headers)
        content = eval(r.text)
        # print(content)
        newlist = content['newslist'][0]
        infos = ''
        if int(content['code']) == 200:
            if newlist['info'] == '工作日':
                infos = '\n' + '今天是工作日,要写日报!!!!' + '\n'
                return infos
            elif newlist['info'] == '双休日':
                infos = '今天是双休日,要写下周的工作计划'
                return infos
            elif newlist['tip'] != '':
                infos = '国家法定节假日,可以好好休息,别忘了周工作计划和月工作计划'
                return infos
            else:
                pass
        else:
            infos = '接口错误'
            return infos
        # print(infos)
        return infos
class YouJia:
    def __init__(self):
        self.url = 'http://api.tianapi.com/txapi/oilprice/index?key=自己的&prov=河南'
        self.data = ''
    def money(self):
        r = requests.get(url=self.url).text
        content = eval(r)
        #print(content)
        if content['code'] == 200:
            print(content['newslist'][0]['p92'])
            self.data = '92号油价:' + content['newslist'][0]['p92'] + '  95号油价:' + content['newslist'][0]['p95']
        else:
            self.data = '油价接口错误!!!'
        return self.data

class Weather():
    def __init__(self):
        self.data={
            "location":"城市",
            "key":"自己的"
        }

    def get_url(self):
        # API地址用S6,也可以用V7
        data_list = requests.get('https://free-api.heweather.com/s6/weather/forecast',params=self.data).json()
        daily_forecast = data_list["HeWeather6"][0]["daily_forecast"]
        forecast_list = []
        for i in daily_forecast:
            # print(daily_forecast.index(i))
            forecast = {}
            # 取列表索引
            if daily_forecast.index(i) == 0:
                forecast['时间'] = '今天   '
            elif daily_forecast.index(i) == 1:
                forecast['时间'] = '明天   '
            else:
                forecast['时间'] = '后天   '
            all_forecast = forecast['时间'] + '  白天:'+ i['cond_txt_d'] + '  晚上:' + i['cond_txt_n'] + '  最高温度:'+ i['tmp_max'] + '°C' + '  最低温度:'+ i['tmp_min'] + '°C' + '  风力:' + i['wind_dir'] + i['wind_sc'] + '级' + '!'

            forecast_list.append(all_forecast)
        select_forecast = "".join(forecast_list)
        new_data = select_forecast.replace('!', '\n')
        return new_data

class YiQing:
    def __init__(self):
        self.url = 'http://api.tianapi.com/txapi/ncov/index?key=自己的'
        self.all_data = ''
        self.henan_news = ''
    def request_data(self):
        r = requests.get(self.url).text
        content = eval(r)
        # print(type(content))
        news_list = content["newslist"][0]
        all_news = news_list['news']
        news_num = len(all_news)
        nationwide = all_news[0]['summary']
        # print(nationwide)
        for i in range(news_num):
            if all_news[i]['id'] == 150879:
                self.henan_news = all_news[i]['summary']
                break
            else:
                self.henan_news = '河南—————————暂无疫情'
        self.all_data = nationwide + '\n' + '-'*34 + '\n' + '\n' + self.henan_news
        return self.all_data

class Weixin():
    def __init__(self,myjob_data, youjia_data, tianqi_data, yiqing_data):
        self.corpid = '自己的'
        self.corpsecret = '自己的'
        self.HEADERS = {"Content-Type": "application/json ;charset=utf-8"}
        self.myjob = myjob_data
        self.youjia = youjia_data
        self.tianqi = tianqi_data
        self.yiqing = yiqing_data
    def news(self):
        send_data = self.myjob + '\n' + '-'*34 + '\n' +  self.youjia + '\n' + '-'*34 + self.tianqi + '-'*34 +  self.yiqing
        r = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + self.corpid + '&corpsecret=' + self.corpsecret).text
        # print(r)
        js = json.loads(r)
        token = js['access_token']
        data = {
            'touser': '@all',
            'msgtype': 'text',
            'agentid': 1000002,
            'text':{
                'content': send_data
                },
            'safe': 0,
            'enable_id_trans': 0,
            'enable_duplicate_check': 0,
            'duplicate_check_interval': 1800

                }
        String_testMsg = json.dumps(data)
        wechaturl = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}'
        # print(wechaturl)
        req = requests.post(wechaturl, data=String_testMsg, headers=self.HEADERS)
        # print(req.text)


def main():
    workers = Worker()
    youjias = YouJia()
    tianqis = Weather()
    yiqings = YiQing()
    wechat = Weixin(workers.my_job(), youjias.money(), tianqis.get_url(), yiqings.request_data())
    wechat.news()
if __name__ == '__main__':
    main()

Python爬取制服买家秀小姐姐

这个网站大概是1个月之前在吾爱破解水漫金山某位大神发出来的,当天我就写了爬虫,今天没事又去爬一下看更新了没,发现是空的,网站内容更改了。
所以刚刚又重新用scrapy写了一个整站爬虫,但还是不发出来,省的把网站给爬死了。
复制出来改成单分类爬虫,剩下的想爬取,自己更改!!!

# from ip_proxy import ips
import requests, os, re, random
from lxml import etree


# ip_add = random.choice(ips())
if not os.path.exists('./zhifu'):
    os.mkdir('./zhifu')

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
for i in range(1,4):
    url = 'https://www.ikmjx.com/index.php?g=portal&m=list&a=index&id=3&p=' + str(i)
    r = requests.get(url=url, headers=headers).text
    tree = etree.HTML(r)
    div_list = tree.xpath('/html/body/main/div/div[2]/div')[1:-1]
    for li in div_list:
        a = 0
        src = 'https://www.ikmjx.com' + li.xpath('./div[2]/a/@href')[0]
        titles = li.xpath('./div[2]/a/@title')[0]
        title = titles.replace('?','')
        req = requests.get(url=src, headers=headers).text
        tree1 = etree.HTML(req)
        div1_list = tree1.xpath('/html/body/main/div/div/div/div[3]/p[2]')
        for p in div1_list:
            src_path = p.xpath('./img/@src')
            # print(src_path)
            for img in src_path:
                a = a+1
                img_data = requests.get(url=img, headers=headers).content
                img_path = './zhifu/' + title + '_' + str(a) + '.jpg'
                with open(img_path, 'wb') as fp:
                    fp.write(img_data)
                    # print(img_data, '下载完成!!!')
                    

python企业微信及时通讯api每日定时推送内容

前几天看了@https://www.52pojie.cn/thread-1338005-1-1.html大神的帖子,想到自己也写一个想看的东西推送一下,所以也用他的借口,真的很方便。

企业微信注册以及接口地址可以参照上面的帖子
自己写了天气+当地的疫情+度娘头条热点,挂到自己的服务器,每天定时发送一些内容。
代码写的很烂,大神略过,可以自己添加一些其他的内容发送

import requests, urllib, sys
from lxml import etree

class TopNews():
    def __init__(self):
        self.url = 'http://top.baidu.com/'
        self.headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
            }
    def get_url(self):
        r = requests.get(url=self.url, headers=self.headers)
        r.encoding = 'gb2312'
        tree = etree.HTML(r.text)
        li_list = tree.xpath('//*[@id="hot-list"]/li')
        titles =[]
        for li in li_list:
            num = li_list.index(li) + 1
            title = li.xpath('./a/@title')[0]
            titles.append(str(num) + ':' + title + '!')
        return titles

class Weather():
    def __init__(self):
        self.data={
            "location":"焦作",
            "key":"3f47abf414184e59b5b791b765e38a74"
        }

    def get_url(self):
        # API地址用S6,也可以用V7
        data_list = requests.get('https://free-api.heweather.com/s6/weather/forecast',params=self.data).json()
        daily_forecast = data_list["HeWeather6"][0]["daily_forecast"]
        forecast_list = []
        for i in daily_forecast:
            # print(daily_forecast.index(i))
            forecast = {}
            # 取列表索引
            if daily_forecast.index(i) == 0:
                forecast['时间'] = '今天   '
            elif daily_forecast.index(i) == 1:
                forecast['时间'] = '明天   '
            else:
                forecast['时间'] = '后天   '
            all_forecast = forecast['时间'] + '  白天:'+ i['cond_txt_d'] + '  晚上:' + i['cond_txt_n'] + '  最高温度:'+ i['tmp_max'] + '°C' + '  最低温度:'+ i['tmp_min'] + '°C' + '  风力:' + i['wind_dir'] + i['wind_sc'] + '级' + '!'

            forecast_list.append(all_forecast)
        select_forecast = "".join(forecast_list)
        new_data = select_forecast.replace('!', '\n')
        return new_data


class YiQing:
    # 由urllib2改成requests
    def __init__(self):
        self.host = 'http://ncovdata.market.alicloudapi.com'
        self.path = '/ncov/cityDiseaseInfoWithTrend'
        self.method = 'GET'
        self.appcode = '9f7f40025d254f2593066def83ebdc98'
        self.querys = ''
        self.bodys = {}
        self.url = self.host + self.path

        self.new_appcode = 'APPCODE ' + self.appcode
        self.headers = {
            'Authorization': self.new_appcode
        }
    def get_data(self):
        response = requests.get(url=self.url, headers=self.headers)
        content = response.text
        # with open('./yiqing.html', 'w', encoding='utf-8') as fp:
        #     fp.write(content)
        # 文本转换成字典
        dict_content = eval(content)
        # 从所有数据取出当地省份内容
        new_content = dict_content['provinceArray'][7]
        yiqing_data = '河南最新疫情:' + '\n' + '新增:' + str(new_content['totalDoubtful']) + '\n' + '累计:' + str(new_content['totalConfirmed']) + '\n' + '累计治愈:' + str(new_content['totalCured']) + '\n' + '累计死亡:' + str(new_content['totalDeath'])
        # print(yiqing_dat)
        return yiqing_data

class SendText:
    def __init__(self, newsdata, hewdata, yqdata):
        self.news_data = newsdata
        self.hew_data = hewdata
        self.yq_data = yqdata
        self.corpid = "自己的"
        self.corpsecret = "自己的"
        self.agentid = "自己的"
    def server_j(self):
        text_content = "".join(self.news_data)
        new_content = text_content.replace('!', '\n')
        # print(new_content)
        send_data = str(self.hew_data) + '\n' + self.yq_data + '\n' + '\n' + new_content

        data = {
        "corpid":self.corpid,
        "corpsecret":self.corpsecret,
        "agentid":self.agentid,
        "text":send_data
                }
        req = requests.post('https://api.htm.fun/api/Wechat/text/',data=data)
        # print(req)
        return req
def main():
    news = TopNews()
    heweather = Weather()
    yiqing = YiQing()
    text = SendText(news.get_url(), heweather.get_url(), yiqing.get_data())
    # print(text.server_j())
if __name__ == '__main__':
    main()

阿里云centos7配置MongoDB开启远程连接

系统:阿里云centos 7.7 64位

mongodb版本: 3.4

因为使用yum安装非常方便,所以下文使用yum安装,。

修改yum包管理配置:

vi /etc/yum.repos.d/mongodb-org-3.4.repo // 会自动新建mongodb-org-3.4.repo文件

复制下面配置信息:

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
tips: 不懂linux去恶补下基础知识(vi 编辑器)

安装mongodb

yum install -y mongodb-org // 一路yes安装mongodb

启动mongodb

systemctl start mongod.service // 启动mongodb

停止mongodb

systemctl stop mongod.service // 停止mongodb

重启mongodb

systemctl restart mongod.service // 重启mongodb

设置mongodb开机启动

systemctl enable mongod.service // 设置开机启动

tips: centos 7将service命令 改为systemctl。

mongodb安装成功,默认配置文件路径为:/etc/mongod.conf。执行cat /etc/mongod.conf,查看文件的配置。

配置文件是yaml 语法:

systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log // 日志文件储存路径

storage:
dbPath: /var/lib/mongo // 数据储存路径
journal:
enabled: true

你也可以更改储存路径,更改路径时因先新建相关文件夹和文件,这里不做修改。

本地连接mongodb:

mongo // 连接本地mongodb
show dbs // 查看所有数据库
use mydb // 切换mydb数据库,没有会自动添加
show collections // 查看集合
默认情况下,mongodb是没有安全验证的,都可以连接到该数据库。因为mongodb要求是部署在一个安全的环境中,而不需要验证。

远程连接:

在本地window中,打开cmd,执行mongo 192.168.31.54 (192.168.31.54为你的阿里云服务器外网地址),发现连接不上,网上找了很长时间,终于解决了。

1、在阿里云esc实例中设置安全组,把27017端口开放,mongodb 默认端口为27017,阿里云为了安全端口默认都是关闭的。

2、修改mongodb配置文件:

vi /etc/mongod.conf // 编辑配置文件

net:
port: 27017
bindIp: 127.0.0.1 // mongodb 默认绑定的IP地址

默认情况下,阿里云只绑定了127.0.0.1本地地址,只能本地访问,需要在上面加上阿里云内网地址。
bindIp: 127.0.0.1,阿里云内网地址

重启mongodb服务器:
systemctl restart mongod.service

在本地cmd中再执行
mongo 阿里云外网地址 // 发现现在可以连接上了。

到此本地和远程都可以连接上mongodb了,远程可以用可视化工具Robomongo连接,直接输入阿里云外网地址和端口号27017即可以远程连接成功。虽然远程可以连接上mongodb,但是只要知道阿里云外网地址,谁都可以通过远程连接到数据库,并且修改数据库了的数据,这样是非常不安全的。所以实际部署中是不建议bindIp里面加上阿里云内网地址的,只能通过本地访问。远程连接只是为了能方便用可视化工具Robomongo方便管理数据库。如果要想用Robomongo,又想数据库不能随便被别人连接上可以吗?当然可以,只需开启身份认证。

默认情况下mongodb是关闭身份认证的,开启身份认证需如下步骤:

1、修改mongodb配置文件

vi /etc/mongod.conf // 编辑mongod.conf文件

security: // 去掉security前面#
authorization: enabled // 添加这句开启认证

在网上看了很多教程关于开启身份认证的,使用auth=true发现不行,后来才知道是教程太老了,配置字段都变了,坑的我找了很长时间。

2、添加超级管理员

默认Mongodb是没有管理员等用户信息的,开启身份认证是需要添加用户信息验证。首先添加的应该是admin数据库里的管理员账号,他用于其他数据库的用户添加、修改、删除等一些权限控制。

执行如下操作:

mongo // 本地连接数据库
use admin // 切换到admin数据库,没有会自动添加
db.createUser( // 创建管理员用户
{
user: "admin", // 账号
pwd: "admin", // 密码
roles: [ { role: "root", db: "admin" } ] // 角色:超级管理员,数据库:admin
}
)
创建成功后会输出Successfully added user。。。

重启mongodb

systemctl restart mongod.service

执行

mongo // 连接数据库
show dbs // 显示所有数据库,这步会报错,说没有通过验证。
use admin // 切换到admin数据库
db.auth('admin','admin') // 用上面设置的账号密码登录
如果返回 '1'表示验证成功, 如果是 '0' 表示验证失败

身份验证开启了,在window端cmd中执行下面命令
mongo 阿里云外网地址 // 发现连接失败,因为没有通过验证。
如果执行下面这句
mongo 阿里云外网地址 -u "admin" -p "admin" --authenticationDatabase admin
// 发现连接成功

Robomongo身份认证连接:

切换到Authorization选项,选中Perform authorization,填上Database, user name, password,就可以连接成功了。

mongodb不像mysql,验证的用户对所有数据库都有读写的能力,不同的库需要配置相关的用户信息才能对该库进行读写。比如有个myblog的数据库,需要对其有读写的能力,新建一个具有读写能力的用户。

命令如下:

mongo // 连接数据库
use admin // 切换到admin数据库
db.auth('admin','admin') // auth验证登录
use myblog // 切换到myblog数据库
db.createUser( // 创建普通用户
{
user: "keen", // 账号
pwd: "123", // 密码
roles: [ { role: "readWrite", db: "myblog" } ] // 角色:读写,数据库:myblog
}
)
db.auth('keen', '123') // 使用新建用户keen验证登录

命令示例:

创建管理员
db.createUser({user:"root",pwd:"密码",roles:[{role:"root",db:"admin"}]})

创建其他库
use admin
db.auth('用户名','密码')
切换数据库
use myblog 
创建账号密码
db.createUser({user:"jobs",pwd:"密码",roles:[{role:"readWrite",db:"jobs"}]})
用新建账账户认证
db.auth('jobs','密码')

数据库名和用户名一样容易记

最后要在防火墙加上规则
iptables -I INPUT -p tcp --dport 27017 -j ACCEPT

永久配置在/etc/sysconfig/iptables
加上

-A INPUT -p tcp --dport 27017 -j ACCEPT

到此整个mongodb配置结束了

摘自:https://www.lmlphp.com/user/16830/article/item/550131/

scrapy爬虫框架创建及案例

一、安装,由于本地和服务器全是anaconda,所以安装比较简单:

conda install -c conda-forge scrapy

二、
1、创建一个工程:

scrapy startproject jobs

2、进入jobs/spiders目录,创建一个爬虫文件

scrapy genspider hr tencent.com

完成后如下图(电脑是老古董,pycharm太卡,一直用sublime)
scrapy创建工程

3、执行工程

scrapy crawl hr

linux安装Python3

卸载Python3.6

whereis python3 |xargs rm -frv

安装Python3.9
1、安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel -y

2、下载、解压、进入文件
cd /usr/local
wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
tar -zxvf Python-3.9.2.tgz
mv Python-3.9.2
cd python3

3、配置路径、编译安装
./configure --prefix=/usr/local/python3
make && make install

4、添加软连接(原来python/pip默认的是2,现在替换成3)
备份:
mv /usr/bin/python /usr/bin/python_back
mv /usr/bin/pip /usr/bin/pip_back
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

5、加入环境变量
export PATH=$PATH:/usr/local/python3/bin

6、修复 yum 下载(因为 yum 依赖于python2)
vi /usr/libexec/urlgrabber-ext-down
vi /usr/bin/yum
修改首行python为python2