【问题标题】:Getting duplicates in csv from a spider written in python [closed]从用python编写的蜘蛛获取csv中的重复项[关闭]
【发布时间】:2017-04-19 22:47:49
【问题描述】:

我创建了一个蜘蛛,它按照我的预期收集数据。我现在面临的唯一问题是结果有很多重复。但是,我想在 csv 中写入结果时去掉重复项:

代码如下:

import csv
import requests
from lxml import html

def Startpoint():
    global writer
    outfile=open('Data.csv','w',newline='')
    writer=csv.writer(outfile)
    writer.writerow(["Name","Price"])
    address = "https://www.sephora.ae/en/stores/"
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"level0")]')
    for title in titles:
        href = title.xpath('.//a[contains(@class,"level0")]/@href')[0]
        Layer2(href)

def Layer2(address):
    global writer
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"amshopby-cat")]')
    for title in titles:
        href = title.xpath('.//a/@href')[0]
        Endpoint(href)

def Endpoint(address):
    global writer
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//div[@class="product-info"]')
    for title in titles:
        Name = title.xpath('.//div[contains(@class,"h3")]/a[@title]/text()')[0]
        Price = title.xpath('.//span[@class="price"]/text()')[0]
        metco=(Name,Price)
        print(metco)
        writer.writerow(metco)

Startpoint()

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。

标签: python csv web-crawler


【解决方案1】:

您不需要 csv 模块来写入 csv 文件。指定扩展名就足够了。因此,把你的代码变成

import requests
from lxml import html

delimiter = ";"
file_name = 'data.csv'

def Startpoint():
    address = "https://www.sephora.ae/en/stores/"
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"level0")]')
    for title in titles:
        href = title.xpath('.//a[contains(@class,"level0")]/@href')[0]
        Layer2(href)

def Layer2(address):
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"amshopby-cat")]')
    for title in titles:
        href = title.xpath('.//a/@href')[0]
        Endpoint(href)

def Endpoint(address):
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//div[@class="product-info"]')
    for title in titles:
        Name = title.xpath('.//div[contains(@class,"h3")]/a[@title]/text()')[0]
        Price = title.xpath('.//span[@class="price"]/text()')[0]
        metco=(Name,Price)
        print(metco)
        with open(file_name,'a') as outfile:
            outfile.write(delimiter.join(metco).encode('utf8') + '\n')

with open(file_name,'w') as outfile:
    outfile.write(delimiter.join(["Product Name", "Price"])+'\n')
Startpoint()

应该可以解决问题。请注意.encode('utf8') 部分,它会阻止您的写作过程来自UnicodeEncodeError。此外,请注意open 函数中使用的参数'w''a'。第一个意思是“写”,第二个意思是“追加”。然而,即使从启发式的角度来看,这段代码是有效的,但它远不是“很好”的想法。

【讨论】:

  • 感谢 Tnerual 的回答。你非常接近我的预期。在这种情况下编码不起作用,所以我放弃了那行。所有结果现在都在 csv 文件中,但它们不是位于单独的列中,而是位于由逗号分隔的单个列中。你能把它分成两列吗?谢谢。这是该输出的链接:“dropbox.com/s/okb7hxocmv5lbax/data.csv?dl=0
  • @SMth80 您可以尝试更改名称为delimiter 的变量。将分号 ";" 更改为逗号 ","
  • 它可以工作,但是当它看到价格超过千时会遇到问题,因为你知道千是这样写的“1,000”。因此,您的代码一旦遇到“,”就会创建一个单独的列。无论如何,我已经修复了它并在我的第一篇文章中重写了更正的那个。你能告诉我如何在用 csv 写作时摆脱重复。谢谢。
  • @SMth80 这正是我不喜欢使用逗号作为分隔符的原因。由于看起来您使用 excel 来可视化您的 csv 文件,请注意您实际上可以告诉它要考虑什么作为分隔符。它直接与逗号一起使用,因为它(通常)是默认的。要控制重复项,您可以创建一个列表,其中每个要写入的元素只有在不存在时才会出现。然后,您将不得不遍历此列表以编写内容。此外,UnicodeEncodeError 不会出现在 python3.+ 中,即您使用的 python 版本。
  • @SMth80 此外,请注意,您不得大幅更改问题的标题以获得全新的相关答案。我回答了你的问题,我的回答满足了你的需要,你声明它是正确的。如果您还有其他问题要问,请创建/打开一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多