Skip to content
Advertisement

python web scraping data to csv

I was trying to use python web scraping then output a csv file but the print format is not matching csv format.

outputenter image description here

how to print this expecting results? enter image description here

Thanks

Below is my script

JavaScript

Advertisement

Answer

The first problem is you use “writerows”, which will lead csv write become several rows as it can. So when your text is “2021/12/23”, the converter will become [‘2’, ‘0’, ‘2’, ‘1’, ‘/’, ‘1’, ‘2’, ‘/’, ‘2’, ‘3’], and write each row with one char. Same problem as the price. So we use “writerow” and save row data as a list to prevent csv convert our data to multiple rows.

The second is use .text in BeautifulSoup will record all the text including whitespaces and lead csv behavior unpredictable. So I will delete all whitespace and # first to prevent this situation.

Here is the modified code

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement