Python 网页抓取 - 保存变量
Python Web scraping - save variable
我正在尝试在 Python 中构建一个程序,以便每小时抓取一个页面,并在添加新内容时通过 Telegram 向我发送消息。
我制作了这段代码:
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)
它有效,但每次我 运行 代码时,变量链接开始变黑。如何保存带有新链接的变量并多次重复使用?
每次 运行 一个 python 脚本时,所有变量都会重新初始化,因此您不能使用之前 运行.
的值
我会做什么:将链接保存到一个文件并在每次 运行 脚本时读取该文件。
我正在尝试在 Python 中构建一个程序,以便每小时抓取一个页面,并在添加新内容时通过 Telegram 向我发送消息。
我制作了这段代码:
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)
它有效,但每次我 运行 代码时,变量链接开始变黑。如何保存带有新链接的变量并多次重复使用?
每次 运行 一个 python 脚本时,所有变量都会重新初始化,因此您不能使用之前 运行.
的值我会做什么:将链接保存到一个文件并在每次 运行 脚本时读取该文件。