Skip to main content
 首页 » 编程设计

python之通过 python 自动化 .get 请求

2024年12月31日8davidwang456

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

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

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

请您参考如下方法:

使用 url 构建一个文本文件。

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 

获取url并处理:

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