I've created a script to parse few data points from an htmlfile link and write the same to a csv file according to this format.
I do locate the fields accordingly using the selectors I've already defined within the script, but I can't stratify the output in the right way so that I can write them later to a csv file.
location of data points:
Nature of association
`from 1st table`
Purpose
In cash (Previous balance)
`from 2nd table`
Donor Name
Address
`from 3rd table`
Country Name
Amount
Here is what I've tried (I suppose the htmlfile link works):
import requests
from bs4 import BeautifulSoup
file_link = 'https://filebin.redpill-linpro.com/zj2qqc27va5fatm0/index.html'
res = requests.get(file_link)
soup = BeautifulSoup(res.text,"lxml")
nature_of_asso = soup.select_one("td:contains('Nature of association') + td").get_text(strip=True)
for purpose_tr in soup.select("table:has(> tr > td:nth-of-type(1) + td:contains('Purpose')) tr")[3:]:
try:
purpose = purpose_tr.select_one('td:nth-of-type(2)').get_text(strip=True)
except AttributeError: purpose = ""
try:
in_cash = purpose_tr.select_one('td:nth-of-type(3)').get_text(strip=True)
except AttributeError: in_cash = ""
print(purpose,in_cash)
for donor_tr in soup.select("table:has(> tr > td:nth-of-type(1) + td:contains('Donor Name')) tr")[2:]:
try:
donor_name = donor_tr.select_one('td:nth-of-type(2)').get_text(strip=True)
except AttributeError: donor_name = ""
try:
address = donor_tr.select_one('td:nth-of-type(3)').get_text(strip=True)
except AttributeError: address = ""
print(donor_name,address)
for country_tr in soup.select("table:has(> tr > td:nth-of-type(1) + td:contains('Country Name')) tr")[1:]:
try:
country = country_tr.select_one('td:nth-of-type(2)').get_text(strip=True)
except AttributeError: country = ""
try:
amount = country_tr.select_one('td:nth-of-type(3)').get_text(strip=True)
except AttributeError: amount = ""
print(country,amount)
How can I arrange output as per the image above in order to write the same to a csv file?
