简体   繁体   中英

Python Web scraping - save variable

I'm trying to build a program in Python to scrape a page every hour and send to me a message on Telegram if something new is added.

I made this code:

web_url = 'https://aaaa'
parameters = {'q':'bbbb', 'from':'cccc'}

bot_key = 'dddd'
chat_id = 'eeee'

scraping = requests.get(web_url, params=parameters, timeout = 5)
website_content = BeautifulSoup(scraping.content, 'html.parser')
match = website_content.findAll(name='a', attrs={'class':'ffff'}, href=True)

links = []

for i in match:
    z = i.get('href')
    if z not in links:
        parameters = {'chat_id':chat_id, 'text':z}
        requests.post(f'https://api.telegram.org/bot{bot_key}/' + 'sendMessage',data=parameters)
        links.append(z)

It works, but every time I run the code, the variable links started black. How can I save the variable with new links and reuse it many times?

Every time you run a python script, all variables are re-initialized, so you can't use values of the previous run.

What i would do: save the links to a file and read that file every time you run the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM