循环到CSV,导致python的行不平坦

发布于 2025-01-30 19:28:20 字数 1049 浏览 3 评论 0原文

仍在学习Python,如果这是一个非常明显的错误,则很抱歉。不过,我一直在尝试弄清楚它几个小时,并认为我会看看是否有人可以提供帮助。

我已经刮了一个曲棍球网站的冰滑冰名称和价格,并将其写给了CSV。唯一的问题是,当我将其写入CSV时,“名称列的行”(列出为齿轮)和价格列未对齐。它进行:

  • Gear Name 1
  • 行太空
  • 价格
  • 行空间
  • 齿轮名称2,

将齿轮和价格排相邻对齐是很棒的。如果有帮助,我也附上了与CSV图片的链接。

import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


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

headers = "Gear, Price"
f.write(headers)

for gear in parser.find_all("div", {"class": "details"}):
    
    gearname = gear.find_all("div", {"class": "name"}, "a")
    gearnametext = gearname[0].text
    
    gearprice = gear.find_all("div", {"class": "price"}, "a")
    gearpricetext = gearprice[0].text

    print (gearnametext)
    print (gearpricetext)

    f.write(gearnametext + "," + gearpricetext)

[排行不平的样子] [1] [1]:https://i.sstatic.net/eg2f2.png

Still learning Python, so apologies if this is an extremely obvious mistake. I've been trying to figure it out for hours now though and figured I'd see if anyone can help out.

I've scraped a hockey website for their ice skate name and price and have written it to a CSV. The only problem is that when I write it to CSV the rows for the name column (listed as Gear) and the Price column are not aligned. It goes:

  • Gear Name 1
  • Row Space
  • Price
  • Row Space
  • Gear Name 2

It would be great to align the gear and price rows next to each other. I've attached a link to a picture of the CSV as well if that helps.

import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


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

headers = "Gear, Price"
f.write(headers)

for gear in parser.find_all("div", {"class": "details"}):
    
    gearname = gear.find_all("div", {"class": "name"}, "a")
    gearnametext = gearname[0].text
    
    gearprice = gear.find_all("div", {"class": "price"}, "a")
    gearpricetext = gearprice[0].text

    print (gearnametext)
    print (gearpricetext)

    f.write(gearnametext + "," + gearpricetext)

[What the uneven rows look like][1]
[1]: https://i.sstatic.net/EG2f2.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2025-02-06 19:28:20

建议使用Python 3建议使用与open(fileName,'w')作为f: and strip()您的文本 write> write> write()到您的文件。

除非您不使用“ A”模式来附加每行,否则您必须在编写的每一行中添加线路破解。

示例
import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


filename = "gear1.csv"
headers = "Gear,Price\n"


with open(filename, 'w') as f:
    f.write(headers)

    for gear in parser.find_all("div", {"class": "details"}):
        gearnametext = gear.find("div", {"class": "name"}).text.strip()
        gearpricetext = gear.find("div", {"class": "price"}).text.strip()
        f.write(gearnametext + "," + gearpricetext+"\n")
输出
Gear,Price
Bauer Vapor X3.7 Ice Hockey Skates - Senior,$249.99
Bauer X-LP Ice Hockey Skates - Senior,$119.99
Bauer Vapor Hyperlite Ice Hockey Skates - Senior,$999.98 - $1149.98
CCM Jetspeed FT475 Ice Hockey Skates - Senior,$249.99
Bauer X-LP Ice Hockey Skates - Intermediate,$109.99

...

Would recommend with python 3 to use with open(filename, 'w') as f: and strip() your texts before write() to your file.

Unless you do not use 'a' mode to append each line you have to add linebreak to each line you are writing.

Example
import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


filename = "gear1.csv"
headers = "Gear,Price\n"


with open(filename, 'w') as f:
    f.write(headers)

    for gear in parser.find_all("div", {"class": "details"}):
        gearnametext = gear.find("div", {"class": "name"}).text.strip()
        gearpricetext = gear.find("div", {"class": "price"}).text.strip()
        f.write(gearnametext + "," + gearpricetext+"\n")
Output
Gear,Price
Bauer Vapor X3.7 Ice Hockey Skates - Senior,$249.99
Bauer X-LP Ice Hockey Skates - Senior,$119.99
Bauer Vapor Hyperlite Ice Hockey Skates - Senior,$999.98 - $1149.98
CCM Jetspeed FT475 Ice Hockey Skates - Senior,$249.99
Bauer X-LP Ice Hockey Skates - Intermediate,$109.99

...

半边脸i 2025-02-06 19:28:20

我注意到gearnametext在字符串中返回2 \ n。您应该尝试方法str.replace()删除正在创建跳到下一行的\ n。尝试:

import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


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

headers = "Gear, Price"
f.write(headers)

for gear in parser.find_all("div", {"class": "details"}):
    
    gearname = gear.find_all("div", {"class": "name"}, "a")
    gearnametext = gearname[0].text.replace('\n','')

    gearprice = gear.find_all("div", {"class": "price"}, "a")
    gearpricetext = gearprice[0].text

    print (gearnametext)
    print (gearpricetext)

    f.write(gearnametext + "," + gearpricetext)

我在循环内更改了:GearNametext = GearName [0] .Text.Replace('\ n',','')

I've noticed that gearnametext returns 2\n inside the string. You should try the method str.replace() to remove the \n which are creating you the jump to the next line. Try with:

import requests
from bs4 import BeautifulSoup as Soup

webpage_response = requests.get('https://www.purehockey.com/c/ice-hockey-skates-senior?')

webpage = (webpage_response.content)
parser = Soup(webpage, 'html.parser')


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

headers = "Gear, Price"
f.write(headers)

for gear in parser.find_all("div", {"class": "details"}):
    
    gearname = gear.find_all("div", {"class": "name"}, "a")
    gearnametext = gearname[0].text.replace('\n','')

    gearprice = gear.find_all("div", {"class": "price"}, "a")
    gearpricetext = gearprice[0].text

    print (gearnametext)
    print (gearpricetext)

    f.write(gearnametext + "," + gearpricetext)

I changed inside the loop the second line for the gear name for: gearnametext = gearname[0].text.replace('\n','').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文