循环到CSV,导致python的行不平坦
仍在学习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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议使用Python 3建议使用
与open(fileName,'w')作为f:
andstrip()
您的文本write> write> write()
到您的文件。除非您不使用“ A”模式来附加每行,否则您必须在编写的每一行中添加线路破解。
示例
输出
...
Would recommend with python 3 to use
with open(filename, 'w') as f:
andstrip()
your texts beforewrite()
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
Output
...
我注意到
gearnametext
在字符串中返回2\ n
。您应该尝试方法str.replace()
删除正在创建跳到下一行的\ n
。尝试:我在循环内更改了:
GearNametext = GearName [0] .Text.Replace('\ n',','')
。I've noticed that
gearnametext
returns 2\n
inside the string. You should try the methodstr.replace()
to remove the\n
which are creating you the jump to the next line. Try with:I changed inside the loop the second line for the gear name for:
gearnametext = gearname[0].text.replace('\n','')
.