美丽的汤-列表中所有项目的CSV格式结果

问题描述:

以下代码段有效",但仅将第一条记录输出到CSV.我正在尝试使其输出相同的输出,但对于all_links列表中的枪支URL列表中的每支枪支.

The below snippet "works" but is only outputting the first record to the CSV. I'm trying to get it to output the same output, but for each gun in the list of gun urls in the all_links list.

我对输出进行打印的任何修改(只是为了查看其工作情况)都会打印相同的结果

Any modification i've made to it with prints for the output (just to see it working) prints the same result

或者如果我列出了gun_details列表并尝试打印,则输出相同的内容.

or if i make a gun_details list and try to print it, get the same one item output.

我将如何打印所有gun_details标签并将其跨度转换为CSV?

How would i go about printing all the gun_details labels and spans into a CSV?

import csv
import urllib.request

import requests
from bs4 import BeautifulSoup

all_links = []


url = "https://www.guntrader.uk/dealers/minsterley/minsterley-ranges/guns?page={}"
for page in range(1, 3):
    res = requests.get(url).text
    soup = BeautifulSoup(res, "html.parser")
    for link in soup.select(
        'a[href*="dealers/minsterley/minsterley-ranges/guns/shotguns/"]'
    ):
        all_links.append("https://www.guntrader.uk" + link["href"])

for a_link in all_links:

    gun_label = []
    gun_span = []

    res = urllib.request.urlopen(a_link)
    # res = requests.get(a_link)
    soup = BeautifulSoup(res, "html.parser")
    for gun_details in soup.select("div.gunDetails"):
        for l in gun_details.select("label"):
            gun_label.append(l.text.replace(":", ""))
        for s in gun_details.select("span"):
            gun_span.append(s.text)

my_dict = dict(zip(gun_label, gun_span))
with open("mycsvfile.csv", "w") as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=None)
    for key in my_dict.keys():
        csvfile.write(f"{key},{my_dict[key]}\n")

尝试以这种方式运行中间部分:

Try running the middle section this way:

for a_link in all_links:
    gun_label = []
    gun_span = []

    res = requests.get(a_link)
    soup = bs(res.content, 'html.parser') #note it's 'res.content', not just 'res'
    for gun_details in soup.select('div.gunDetails'):
        for l in gun_details.select('label'):
            gun_label.append(l.text.replace(':',''))
    for s in gun_details.select('span'):
        gun_span.append(s.text)

    #this block is now indented differently - it's INSIDE the 'for' loop
    my_dict = dict(zip(gun_label, gun_span))
    with open('mycsvfile.csv', 'a') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=None)
        for key in my_dict.keys():
            csvfile.write(f"{key},{my_dict[key]}\n")