1

Recently have found a tiktok account I want personal notifications from when he posts a vid so I'm using BeautifulSoup plus requests to scrape the data and smtplib to get the notification.

It worked perfectly for 16 hours non stop with no error, Then it suddenly started giving the 'method' object is not subscriptable. Really don't know where the error is since the IP haven't been blocked and If I try to print the page all the page is printed. The error I'm getting is :

    Traceback (most recent call last):
 File "likes.py", line 44, in <module>
    check:likes()
 File "likes.py", line 14, in check_likes
    converted_likes=float(title[0:4])
TypeError: 'method' object is not subscriptable

My script is:

import requests
from bs4 import BeautifulSoup
import smtplib 
import time
import datetime



def check_likes():
    URL = 'https://www.tiktok.com/@uchiha_johnzmen?'
    headers = {"user-agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
    page=requests.get(URL, headers=headers);
    soup = BeautifulSoup(page.content, 'html.parser');
    now=datetime.datetime.now()
    title = soup.find(title="Likes").get_text()
    converted_likes = float(title[0:4])
    original_likes=1476
    print (title.strip())
    print (now.hour,':',now.minute)
    if(converted_likes>original_likes):
        send_mail()
        time.sleep(1800)
        original_likes=converted_likes
        print (original_likes)

def send_mail():
    server=smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login('','')
    subject = 'nuevo video, run bitch'
    body = ''
    msg = f"Subject: {subject}\n\n{body}"
    server.sendmail(
        '@gmail.com',
        '@gmail.com',
        msg
    )
    print ('email sent')
    server.quit()



while True:
    check_likes()
    time.sleep(15)

Would really use a helping hand since I don't understand why the number of likes is a non-sucrpitable object or why it stopped working after so many hours.

user3299086
  • 31
  • 1
  • 6
  • Is the behavior intermittent, or is it happening 100% of the time for you now? - I tried running your code, and it works fine for me. Very strange. – CryptoFool Jan 23 '21 at 17:35
  • It's happening every time now! the script can't be launched since the error appears each time – user3299086 Jan 23 '21 at 17:48
  • Can you stop in the debugger at that line and look at the value of `title`? The error message seems to be saying that `title` is an object of type `method`, which makes no sense. – CryptoFool Jan 23 '21 at 17:58
  • +import requests likes2.py: line1: import: command not found from bs4 import BeautifulSoup +from bs4 import BeautifulSoup from: can't read /var/mail/bs4 import smtplib +import smtplib likes2.py: line 3: import:command not found import time: +import time: likes2.py: line 4: import: command not found import datetime +import datetime likes2.py: line 5: import: command not found def check_likes(): likes2.py: line 8 : syntax error near unexpected token `C' likes2.py: line 8 : def check_likes(): – user3299086 Jan 23 '21 at 18:31
  • That's the whole debug console output – user3299086 Jan 23 '21 at 18:31
  • 1
    That looks like you're trying to run your python code as a shell script. I'm not sure what to make of that. I don't see how that has anything to do with the runtime error you mention in your question. – CryptoFool Jan 23 '21 at 19:35
  • yeah, indeed I'm trying to run the script directly from the shell!! Do you know how to run it correctly? – user3299086 Jan 23 '21 at 20:04
  • After compliling the code to an .exe I'm still getting the same error! Thought it would have been solved with the .exe but it isn't – user3299086 Jan 24 '21 at 15:12
  • You either want to run python explicitly, like `python script.py`, or you want to put a "shebang" at the beginning of your file. See [this SO question](https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take) – CryptoFool Jan 24 '21 at 17:08
  • Sorry to keep using your time but the shebang didn't resulted well, any other possible solutions? – user3299086 Jan 24 '21 at 17:55
  • If a shebang didn't work, then I don't understand your problem, because you can always use a shebang to run a python script as though it were an executable. Your original SO question and the discussion we're having in the comments seem to be about two separate problems. Please modify your question or create a second one such that the question(s) itself fully describes whatever you're looking for help with. – CryptoFool Jan 24 '21 at 18:08
  • Just did it, Thanks Steve – user3299086 Jan 24 '21 at 19:40
  • 1
    if `title` is a method, that's only possible if you have missing parens at `title = soup.find(title="Likes").get_text`. Are you sure you're running the latest version of the script? Are you sure you have saved the recent changes? – Thomas Weller Jan 24 '21 at 19:50
  • Just rechecked the script to make sure and yes, I'm running the latest version of the script, also there isn't any missing parens at the line – user3299086 Jan 24 '21 at 19:55

0 Answers0