Issue
Hi there, I have been dealing with something, but it didn't work whatever i tryed. In my csv file values are separated with double quotes. That's why, happened some problem. Csv file is not completely true.
I have been trying to scrape addresses in this page: https://www.worldometers.info/coronavirus/#news
And also numbers are have to be unified. These values separated with comma. Like 99,341,447 but it's have to be 99341447 How can i fix this?
import requests
from bs4 import BeautifulSoup
from csv import writer
response = requests.get('https://www.worldometers.info/coronavirus/#news').content
soup = BeautifulSoup(response,'lxml')
tbody=soup.find('table', id='main_table_countries_today').find('tbody').find_all('tr')[8:11]
with open('corona1.csv','w') as csv_file:
csv_writer = writer(csv_file)
csv_writer.writerow(['countries','total_cases','total_deaths','total_recovered','active_cases','total_cases_in_1m','deaths_in_1m','population'])
for value in tbody:
countries = value.find_all('td')[1].text
total_cases= value.find_all('td')[2].text
total_deaths=value.find_all('td')[4].text
total_recovered=value.find_all('td')[6].text
active_cases=value.find_all('td')[8].text
total_cases_in_1m=value.find_all('td')[10].text
deaths_in_1m=value.find_all('td')[11].text
population=value.find_all('td')[14].text
csv_writer.writerow([countries,total_cases,total_deaths,total_recovered,active_cases,total_cases_in_1m,deaths_in_1m,population])
Solution
To solve the first issue, you must import QUOTE_NONE
from csv
:
from csv import QUOTE_NONE
Then, when you create csv_writer
, you must specify the following additional arguments:
csv_writer = writer(csv_file, escapechar=' ', quoting=csv.QUOTE_NONE)
Regarding the second issue, characters in string can be replaced like this:
total_cases_in_1m = value.find_all('td')[10].text.replace(",", "")
Answered By - Deneb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.