【问题标题】:Python Web Scraper issuePython Web Scraper 问题
【发布时间】:2019-04-05 07:28:31
【问题描述】:

我是编程新手,并尝试通过构建一些小型项目来学习。我有这段代码并且它正在工作,但是当它提取所有信息时,它在 csv 中正确格式化时遇到了问题。在我添加要拉出的价格后,它开始添加奇怪的空格。如果我注释掉价格并将其从 write 中删除,它可以正常工作,但我无法弄清楚为什么我在添加回来时会出现奇怪的空格。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = "https://www.newegg.com/Product/ProductList.aspx?Submit=ENE&N=-1&IsNodeId=1&Description=graphics%20card&bop=And&PageSize=12&order=BESTMATCH"


# Opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()


#html parsing
page_soup = soup(page_html, "html.parser")


# grabs each products
containers = page_soup.findAll("div",{"class":"item-container"})


filename = "products.csv"
f = open(filename, "w")

headers = "brand, product_name, shipping\n"

f.write(headers)

for container in containers:
    brand = container.div.div.a.img["title"]

    title_container = container.findAll("a", {"class":"item-title"})
    product_name = title_container[0].text

    shipping_container = container.findAll("li", {"class":"price-ship"})
    shipping = shipping_container[0].text.strip()

    price_container = container.findAll("li", {"class":"price-current"})
    price = price_container[0].text.strip()

    print("brand: " + brand)
    print("product_name: " + product_name)
    print("Price: " + price)
    print("shipping: " + shipping)


    f.write(brand + "," + product_name.replace(",", "|") + "," + shipping + "," + price + "\n")

f.close()

【问题讨论】:

  • 您想向我们展示错误吗?
  • 我添加了一张带有价格的 csv 图片,您可以在图片中看到由于某种原因它没有添加价格列,而是留出空间并移动信息

标签: python web-scraping


【解决方案1】:

您可以像下面显示的那样写入 csv 文件。它产生的输出应该服务于目的。 Check out this documentation 了解清楚。

import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup

my_url = "https://www.newegg.com/Product/ProductList.aspx?Submit=ENE&N=-1&IsNodeId=1&Description=graphics%20card&bop=And&PageSize=12&order=BESTMATCH"

page_html = urlopen(my_url).read()
page_soup = BeautifulSoup(page_html, "lxml")

with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    writer.writerow(["brand", "product_name", "shipping", "price"])

    for container in page_soup.findAll("div",{"class":"item-container"}):

        brand = container.find(class_="item-brand").img.get("title")
        product_name = container.find("a", {"class":"item-title"}).get_text(strip=True).replace(",", "|")
        shipping = container.find("li", {"class":"price-ship"}).get_text(strip=True)
        price = container.find("li", {"class":"price-current"}).get_text(strip=True).replace("|", "")

        writer.writerow([brand,product_name,shipping,price])

【讨论】:

    【解决方案2】:

    您会收到新行和垃圾邮件字符,因为这是您从 BS4 获得的数据:它不是写作过程的产物。这是因为您正在尝试获取列表项中的所有文本,而其中有很多内容。查看页面,如果您只想获取价格,您可以将列表中 strong 标签的文本与 sup 标签的文本连接起来,例如price = price_container[0].find("strong").text + price_container[0].find("sup").text。这将确保您只挑选出您需要的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2021-10-19
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多