首页 > 解决方案 > 自动化 Boring Stuff Lucky 批处理文件和命令行问题

问题描述

我对项目有很大的问题:“我感觉很幸运”来自用 Python 自动化无聊的东西的谷歌搜索,我搜索了几个小时的答案,但我无法处理 bat 文件问题。到目前为止,我有这样的事情:

#! python
import sys, webbrowser, bs4, requests

headers = {'User-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) 
             AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 83.0.4103.61 Safari/537.36'}

link = 'https://www.google.pl/search?q=' + ' '.join(sys.argv[1:]
res = requests.get(link, headers=headers)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'lxml')
links = soup.select(".r > a[href]")


for i in range(3):
   webbrowser.open(links[i].attrs['href'])

当我尝试通过 PyCharm 使用其他链接(例如下面的示例)运行它时,没关系,来自谷歌搜索的 3 个新标签正在打开搜索“python”

res = requests.get('https://www.google.pl/search?q=python', headers=headers)

但问题是我无法通过命令行通过 .bat 文件使用一些参数打开它,当我尝试通过双击打开它时它甚至无法打开(当然,当上面的测试示例在我的文件中时)。

任何人都可以帮忙吗?我的 .bat 文件如下所示:

"C:\something\something\python.exe" "D:\something\something\luckysearch.py" %1

标签: pythonbatch-filecommand-linebeautifulsoup

解决方案


推荐阅读