python - 从列中获取 URL 并粘贴到 chrome 中
问题描述
我有一个 Excel 文件,其中有一列填充了 +4000 个 URL,每个 URL 位于不同的单元格中。我需要使用 Python 用 Chrome 打开它并从网站上抓取一些数据。在excel中过去。
然后对下一个 URL 执行相同的步骤。你能帮我解决这个问题吗?
解决方案
将 excel 文件导出到 csv 文件,从中读取数据
def data_collector(url):
# do your code here and return data that you want to write in place of url
return url
with open("myfile.csv") as fobj:
content = fobj.read()
#below line will return you urls in form of list
urls = content.replace(",", " ").strip()
for url in urls:
data_to_be_write = data_collector(url)
# added extra quotes to prevent csv from breaking it is prescribed
# to use csv module to write in csv file but for ease of understanding
# i did it like this, Hoping You will correct it by yourself
content = "\"" + {content.replace(url, data_to_be_write) + "\""
with open("new_file.csv", "wt") as fnew:
fnew.write(content)
运行此代码后,您将new_file.csv
使用 Excel 打开它,您将获得所需的数据来代替 url
如果您希望您的 url 带有数据,只需将其附加到用冒号分隔的字符串中的数据即可。
推荐阅读
- angular - 在 Angular 中实现外部 Cludo 脚本的最佳实践
- html - 在 HTML5 页面中显示德语重音字符的问题
- android - Android Studio - 移除的依赖不会从外部库中移除
- ffmpeg - 作为服务运行时,ffmpeg -hls_time 选项无法正常工作
- android - 我可以在一个 HTTP 请求中从谷歌距离矩阵 api 获取所有数据吗?
- swift - 带有自动布局的 uiscrollview 的动态内容未按预期工作
- php - 未捕获的 ArgumentCountError。分解意义
- php - PSR-4 自动加载选项没有在我的项目中加载类?
- c# - 脚本任务 C# 中的命令超时
- google-cloud-platform - 是否可以使用 gcloud CLI 启动 GCP 市场条目?