【问题标题】:Automate .get requests via python通过 python 自动化 .get 请求
【发布时间】:2018-10-27 03:07:09
【问题描述】:

我有 a python script 抓取页面,使用 jinja2 模板引擎输出适当的 HTML,感谢各位好心人和 The Coding Den Discord 的人们,我终于开始工作了。

我希望自动执行我在文件顶部发出的 .get 请求。

我有数以千计的 URL,我希望在其上运行此脚本。有什么好的方法来解决这个问题?我尝试过使用 URL 数组,但 requests 对此表示反对。它抱怨必须是一个字符串。所以看来我每次都需要遍历 compiledUrls 变量。任何有关该主题的建议将不胜感激。

【问题讨论】:

  • 把所有的url放到一个文本文件中,然后逐行读取

标签: python python-3.x python-requests


【解决方案1】:

使用网址构建一个文本文件。

urls.txt

https://www.perfectimprints.com/custom-promos/20267/Pizza-Cutters1.html
https://www.perfectimprints.com/custom-promos/20267/Pizza-Cutters2.html
https://www.perfectimprints.com/custom-promos/20267/Pizza-Cutters3.html
https://www.perfectimprints.com/custom-promos/20267/Pizza-Cutters4.html
https://www.perfectimprints.com/custom-promos/20267/Pizza-Cutters5.html

获取网址并处理

with open("urls.txt") as file:
    for single_url in file:
        url = requests.get(single_url.strip())
        ..... # your code continue here

【讨论】:

  • 这看起来正是我所需要的。让我试试看。跟进:我需要将我在脚本末尾生成的文件保存到一个单独的文件夹中(index.html 和 products.json 到一个名为 Pizza-cutters 的文件夹中)。我该怎么做?
  • compiledUrls = open("urls.txt").splitlines() AttributeError: '_io.TextIOWrapper' object has no attribute 'splitlines' 尝试您的代码时遇到的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多