Python - 关于 For 循环的帮助

Python - Help about For Loop

我有这个 python 代码并且运行良好

我正在使用 xpath 获取计划的标题并使用此名称创建目录。

import requests
from lxml import html
import csv
import os

resp = requests.get(
                    url="https://www.architecturaldesigns.com/house-plans/prairie-style-home-plan-14469rk",
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

tree = html.fromstring(html=resp.text)

title = tree.xpath("//div[@class='title-text']/text()")[0]
print(title)


dirName = title.replace('\n', '')

if not os.path.exists(dirName):
    os.mkdir(dirName)
    print("Directory " , dirName ,  " Created ")
else:    
    print("Directory " , dirName ,  " already exists")

我是 Python 的新手,我正在尝试学习 For 循环... 有人可以帮我把这段代码放在一个循环中吗? 我正在尝试以下代码,但它不起作用 =(

import requests
from lxml import html
import csv
import os

urls = ['https://www.architecturaldesigns.com/house-plans/prairie-style-home-plan-14469rk','https://www.architecturaldesigns.com/house-plans/this-plan-exudes-tradition-59348nd']

for url in urls:

resp = requests.get(
                    url=urls,
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

tree = html.fromstring(html=resp.text)

title = tree.xpath("//div[@class='title-text']/text()")[0]
print(title)


dirName = title.replace('\n', '')

if not os.path.exists(dirName):
    os.mkdir(dirName)
    print("Directory " , dirName ,  " Created ")
else:    
    print("Directory " , dirName ,  " already exists")

我创建了一个名为 "urls" 的列表,并尝试使 for 循环... 我想在此列表中添加一些 url,因为我需要脚本为每个 url 创建 1 个目录...这可能吗?有人可以帮助我吗?

非常感谢,伙计们!

for url in urls:

    resp = requests.get(
                        url=url,
                        headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    )
...

在调用 get() 时,您应该使用 url 而不是 urls

您需要缩进 request.get 行。

For url in urls:

# indent this line
  resp = requests.get(
                    url=urls,
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

你的缩进不完全正确,其余代码应该在 for 循环中 运行 所以它对两个 url 都这样做,否则只有最后一个会被保存,其他的将被覆盖和丢弃由最近的一个。所以你的代码实际上应该是这样的

import requests
from lxml import html
import csv
import os

urls = ['https://www.architecturaldesigns.com/house-plans/prairie-style-home-plan-14469rk','https://www.architecturaldesigns.com/house-plans/this-plan-exudes-tradition-59348nd']

for url in urls:

    resp = requests.get(
                    url=url,
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    )

    tree = html.fromstring(html=resp.text)

    title = tree.xpath("//div[@class='title-text']/text()")[0]
    print(title)


    dirName = title.replace('\n', '')

    if not os.path.exists(dirName):
        os.mkdir(dirName)
        print("Directory " , dirName ,  " Created ")
    else:    
        print("Directory " , dirName ,  " already exists")
        continue

注意缩进的变化。如果没有这个,当 for 循环 运行s 时,在为 title 赋值后,它会被循环的下一次迭代覆盖,因此当 for 循环完成时,它只分配一个值,即最后一个值迭代,这意味着只有迭代的最后一个值才会出现在您的结果中。因此,将所有内容都放在 for 循环中(更正您的缩进),您应该可以开始了。希望对您有所帮助