如何使用输入参数自动创建脚本?
我有一个文件夹,里面有类似的脚本,可以从 RSS 提要中抓取 google 警报。
除了 url 末尾的变量 uniqueurl
之外,所有文件都完全相同
url = 'https://www.google.co.in/alerts/feeds/*uniqueurl*'
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
output = []
for entry in soup.find_all('entry'):
item = {
'Title': entry.find('title', {'type': 'html'}).text,
'Pubdate': entry.find('published').text,
'Content': entry.find('content').text,
'Link': entry.find('link')['href']
}
output.append(item)
df = pd.DataFrame(output)
df.to_csv('google_alert.csv',index=False)
如何运行像 python create.py uniqueurl
这样的命令,它仅使用以下内容生成上述文件url 变量更新了命令中传递的内容?
I have a folder of similar-looking scripts which scrape google alerts from their RSS feeds.
All the files are exactly the same except the variable uniqueurl
at the end of url
url = 'https://www.google.co.in/alerts/feeds/*uniqueurl*'
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
output = []
for entry in soup.find_all('entry'):
item = {
'Title': entry.find('title', {'type': 'html'}).text,
'Pubdate': entry.find('published').text,
'Content': entry.find('content').text,
'Link': entry.find('link')['href']
}
output.append(item)
df = pd.DataFrame(output)
df.to_csv('google_alert.csv',index=False)
How do I run a command like python create.py uniqueurl
which generates the above file with just the url variable updated with what is passed in the command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次执行脚本时,使用 sys.argv 捕获您想要在运行时获取的任何变量。
然后,在运行脚本时,您可以传递一个将分配给这些变量的值,如下所示:
python script.py uniqueUrl Category
- 这样您就不需要存储多个脚本并在每次想要在代码中进行一些小的更改时重新生成它们。Use sys.argv to capture any variables you want to get at runtime each time the script is executed.
Then when running a script you can pass a value that will be assigned to those variables like so:
python script.py uniqueUrl Category
- that way you don't need to store multiple scripts and regenerate them every time you want to make some small difference in the code.