Home > Enterprise >  (Selenium Python) Why xpath data that is separated by comma on website, is separated by column in th
(Selenium Python) Why xpath data that is separated by comma on website, is separated by column in th

Time:07-08

I'm currently doing a web-scrapping to Excel by using Selenium Python. I had a problem where I tried to scrap a data that is separated by commas (it's a house address) and I was able to successfully transfer it into Excel. However, the data is not in one cell, instead, they are separated by columns (due to the commas). My goal is to get the data in one column including the commas from the address. For example:

Data on website: No 3, Jalan Kuching, 43500, Kuala Lumpur, Malaysia.

Data scrapped to excel:

[1] [1]: https://i.stack.imgur.com/im0Nq.png

Here's a sample of the code:

filename = "data.csv"
FileExist = 1
if not path.exists(filename):
    FileExist = 0
file = open(filename, "a", encoding="utf-8")
if FileExist == 0:
    file.write("Autopay, Address")

autopay = driver.find_element(by=By.XPATH, value=xpath["autopay1"]).text
address = driver.find_element(by=By.XPATH, value=xpath["address1"]).text

time.sleep(20)
file.write("\n"   autopay   ","   address)

CodePudding user response:

If the cells in your CSV file can contain commas, you should enclose the cells in double quote characters ", e.g.

file.write("\n\""   autopay   "\",\""   address   "\"")
  • Related