CSV output of all the results of scraping multiple-page tables

Asked 2 months ago, Updated 2 months ago, 2 views

I am able to scrap a multi-page table, but I am having trouble because I cannot output all of the results to CSV.
In the code we tried, only the table obtained from the second URL is printed as a CSV.
Please tell me how to output all tables from all URLs to the CSV.

import csv
import urllib.request
from bs4 import BeautifulSoup
import pandas aspd

For urlin urls:
  bsObj=BeautifulSoup(html, "html.parser")
  table=bsObj.findAll("table", {"class": "rankingTable"})[0]
  tables = table.findAll("tr")

  with open("newbooks.csv", "w", encoding='utf-8') as file:
    For row intables:
        csvRow = [ ]
        for cell in row.findAll (['td', 'th']
        writer.writerow (csvRow)



2022-09-30 14:18

2 Answers

The current code specifies overwrite mode w when opening the file in open, so I think the loop action resets every time you open the file, and only the last result is output.

Try additional modea when you open the file as follows:

open("newbooks.csv", "a", encoding='utf-8')

2022-09-30 14:18

Pandas has a method called pandas.read_html, so you can use it.

import pandas as pd

urls = [

df = pd.concat(
   for urlin urls],
df.to_csv('newbooks.csv', encoding='utf-8', index=False)


 Ranking, Book Name, Author, Publisher, Unit Price, Release Month, ISBN
1. Public Rebellion, Jose Ortega Lee Gasset, Iwami Bookstore, 1,070 Yen, April 2020, 9784003423110
2, Trapedium, Kazumi Takayama, KADOKAWA, 680 yen, April 2020, 9784041026441
3. Philosophy of study, Masaya Chiba, Bungei Spring Autumn, 700 Yen, March 2020, 9784167914639

28, Wabasuke no Haku, Yasuhide Saeki, Bungei Spring Autumn, 730 yen, May 2020, 9784167914943
29. Thinking about the history of the dialogue book, Ryotaro Shima, Shunaki Bungei, 670 yen, May 2020, 9784167914998
30, Marugoto no Tsuzui-san, Tsuzui, Bungei Spring Autumn, 900 Yen, May 2020, 9784167915001
1. What has education evaluated? Yuki Honda, Iwami Bookstore, 840 yen, March 2020, 9784004318293
2. There are no real infectious diseases, Kentaro Iwata, Shueisha International, 980 yen, April 2020, 9784797680522
3,5G, Hiroyuki Morikawa, Iwami Bookstore, 860 yen, April 2020, 9784004318316

2022-09-30 14:18

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.