ConfigParser.NoSectionError: No section:错误处理

这两天在撸一个监控推特大v更新后调用脚本自动给大v评论来涨粉的一个脚本,在配置文件ini路径坑了我几个小时。
ConfigParser.NoSectionError: No section 这报错,如果你ini文件的section存在但是读取不到,一般就一种情况:写在代码里的ini文件的路径错误。
我在mac和win系统里都使用了os.path.join(os.path.dirname(file)来直接获取文件所在的文件夹。但mac里正常,win里就报错。

def inipath():
    ini_path = os.path.join(os.path.dirname(__file__) + "/tweetID.ini")
    return ini_path

最后实在无奈,因为只是自己用,所以用绝对路径解决了😂😂
付上监控推特大v代码,评论部分用post没法提交一直解决不了,就用selenium先对付了。


================================= 补充开始 =================================

os.path.abspath()可以正常跨平台使用,如下:

with open(os.path.abspath('./get_msg_from_groups.csv'), 'r', encoding='utf-8-sig') as f2:

================================= 补充完毕 =================================

import csv
import time
import requests
import json
import configparser
import os

# 拼接配置文件路径
def inipath():
    ini_path = os.path.join(os.path.dirname(__file__) + "/tweetID.ini")
    return ini_path

# url和headers公用
def urlandheader():
	# 这个请求链接,在Network里的Usertweets开头的文档里
    url = 'https://twitter.com/i/api/graphql/oFye9u4wTHv8E7oec5UaLA/UserTweets?'
    # cookies放在header内,分开写不起作用,不知什么原因
    header = {'自己的headers'}
    return url, header


# 获取link_str
def readlink():
    link_total_list = []
    # 逐行从csv表中读取username和link,读取结果完成后是嵌套列表
    csv_path = "C:/Users/Administrator/Desktop/detect/links.csv"
    with open(csv_path, 'r') as f1:
        reader = csv.reader(f1)
        for link_total_each in reader:
            link_total_list.append(link_total_each)
    return link_total_list


# 获取前次保存的数据
def getpreviousID(usrname):
    ini_path = inipath()
    config = configparser.ConfigParser()
    config.read(r'C:\Users\Administrator\Desktop\detect\config.ini', encoding="utf-8")
    # 获取大户的tweetID数据
    tweet_ID_previous = config.get('usertweetid', usrname)
    print('{}前次推特ID数据:{}'.format(usrname, tweet_ID_previous))
    return tweet_ID_previous


# requests get用户本次的ID数据
def getnowID(usrname, usrlink):
    requestsheader = urlandheader()
    url = requestsheader[0]
    header = requestsheader[1]
    url_total = url + usrlink
    re = requests.get(url_total, headers=header, )
    re_json = json.loads(re.text)
    # 获取该账户的推特列表,并只取前3
    entryID_list = re_json['data']['user']['result']['timeline']['timeline']['instructions'][0]['entries'][:3]
    # 遍历获取每个推特的ID
    tweet_ID_now = str()
    for entryID_each in entryID_list:
        entryID_ID = entryID_each['entryId']
        # 排除掉系统推广的推特,否则每次获取的tweet_ID_now的结果都不同
        if 'promotedTweet' in entryID_ID:
            pass
        else:
            tweet_ID = entryID_each['sortIndex']
            tweet_ID_now += tweet_ID
    print('{}本次推特ID数据:{}'.format(usrname, tweet_ID_now))
    return tweet_ID_now


# 当前ID数据写入ini文件
def writeini(usrname, tweet_ID_now):
    # 将获取到的数据逐个写入ini配置文件
    nowIDdata = tweet_ID_now
    config_user = configparser.ConfigParser()
    # win系统中的绝对路径
    config_user.read(r'C:\Users\Administrator\Desktop\detect\config.ini')
    config_user.set('usertweetid', usrname, nowIDdata)
    config_user.write(open(inipath(), "w"))


# 总控制及异常处理方法
def control():
    while True:
        usrlinkdatalist = readlink()
        for usrlinkdataeach in usrlinkdatalist:
            usrname = usrlinkdataeach[0]
            usrlink = usrlinkdataeach[1]
            # 爬取数据异常处理
            try:
                tweet_ID_now = getnowID(usrname, usrlink)
                tweet_ID_previous = getpreviousID(usrname)
                if tweet_ID_now == tweet_ID_previous:
                    print('{}的推特未更新!'.format(usrname))
                else:
                    counttime = 0
                    print('{}的推特已更新!准备发表评论...'.format(usrname))
                    # 评论模块,调用获取
                    comments = Applications()
                    comments.search(usrname)
                    # 写入配置文件异常处理
                    writeini(usrname, tweet_ID_now)
                    while counttime < 60:
                        print('{}的推特已更新!准备发表评论...提醒倒计时:{}秒'.format(usrname, 59 - counttime))
                        time.sleep(1)
                        counttime += 1
                    print('已经在{}的最新推特完成发表评论!'.format(usrname))
            except BaseException:
                counttime = 0
                while counttime < 60:
                    print('{}的推特爬虫数据获取频繁!忽略该用户,技能冷却中...倒计时{}秒'.format(usrname, 59 - counttime))
                    time.sleep(1)
                    counttime += 1
            time.sleep(1)


if __name__ == "__main__":
    control()

ini文件配置内容,更多opthon自己添加即可:

[usertweetid]
elonmusk = 146395983014258689014638287651512729621463536507537526787
cz_binance = 146461228815380889814644738798464163841464401884928692226
vitalikbuterin = 146395327480603854214631993289950904431463199328995090442
aantonop = 146425264328350515414635857292551946271463585729255194626
naval = 14647887679531540581464711456499269632
justinsuntron = 146486709000794931214648145002977239071464814500297723906
adam3us = 14647541419773542531464753913278644232
woonomic = 14648228177241374721464596320983814153

如果有朋友能够破解推特的评论框的post,欢迎交流~
😂自己记性太差,做个记录😂

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爬吧爬吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值