Save CSV when scraped (Horse Racing List)

Asked 2 years ago, Updated 2 years ago, 16 views

Hello
I would like to ask you a question for your help.

I'm still a beginner at python, so I might ask you some strange questions, but
Thank you for your understanding.

I'm currently thinking of scraping the race ticket.
So I want to save the scraped results to CSV.
You have entered the following code:

The original page for scraping is
http://race.netkeiba.com/?pid=race&id=c201605050211&mode=shutuba
Here's the list of candidates.

===========================================

import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
# Specifying URLs
html=urlopen("http://race.netkeiba.com/?pid=race&id=c201605050211&mode=shutuba")
bsObj=BeautifulSoup(html, "html.parser")
# Specify a table
table=bsObj.findAll("table", {"class": "race_table_01nk_tb_common shutuba_table"})[0]
rows=table.findAll("th")

csvFile=open("keiba.csv", 'wt', newline=', encoding='utf-8')
writer=csv.writer(csvFile)
try:
 For row in rows:
  csvRow = [ ]
  for cell in row.findAll (['td', 'th']
      csvRow.append(cell.get_text())
  writer.writerow (csvRow)
finally:
    csvFile.close()

============================================

If you type like this and press Enter,

80
559
565
596
634
629
622
561
578
559
633
627
545
642
575
628

These numbers were printed.

I'm not sure what's going on, so I'd like to ask everyone
Let me ask you a question.

Also, I have a CSV (keiba.csv) file for output on my desktop.

How can I enter it directly into CSV?

In the future, I would like to use SQL to store the data of the running list in the database.
Please let me know if there are any web pages that I can refer to.

I'm sorry for the long delay, but please check it.

python

2022-09-29 20:42

1 Answers

At times like this, I'll cut off the non-essential parts and see if they can be reproduced. Specifically, you can ignore the source of the data.

import csv
f=open('opened.csv', 'wt', newline=', encoding='utf-8')
writer=csv.writer(f)
for in ["here", "there"]:
    writer.writerow(i)
f.close()

If you type this into the interactive environment, the standard output will be 9 and 11, but it should have been written to the file itself ([[here], [there]].

$cat opened.csv
h, e, r, e
t,h,e,r,e

The problem is what this number is, but writer.writerow() is not mentioned in the document, but it seems to return the number of characters written. (csv.py reads _csv.c, running from this file?pt.py) does not print the return value, and I think you can basically ignore it. If you want to do it interactive and feel depressed because the print is on the screen, for example,

You can discard the value in .


2022-09-29 20:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.