0

I am trying to scrape data from a webpage and save the scraped text in JSON format. I have reached until the step where i can gather text which i want but then i cant save it in expected format. Csv or txt format is also sufficient if possible

Please help me how to save scraped text in JSON. Here is my code which i have extracted

        for k in range(0, len(op3)):
            selectweek.select_by_index(k)
            table = driver.find_element_by_xpath("//table[@class='list-table']")
            for row in table.find_elements_by_xpath('//*[@id="dvFixtureInner"]/table/tbody/tr[2]/td[6]/a'):
                row.click()
                mainpage = driver.window_handles[0]
                print(mainpage)
                popup = driver.window_handles[1]
                driver.switch_to.window(popup)
                time.sleep(3)

                #Meta details of match
                team1 = driver.find_element_by_xpath('//*[@id="match-details"]/div/div[1]/div/div[2]/div[1]/div[1]/a')     #Data to save
                team2 = driver.find_element_by_xpath('//*[@id="match-details"]/div/div[1]/div/div[2]/div[3]/div[1]/a')     #Data to save
                ht = driver.find_element_by_xpath('//*[@id="dvHTScoreText"]')     #Data to save
                ft = driver.find_element_by_xpath('//*[@id="dvScoreText"]')       #Data to save

2 Answers 2

1

Create dictionary and convert it into JSON format using json module.

import json

dictionary = {"team1" : team1, "team2": team2, "ht": ht, "ft": ft}

json_dump = json.dumps(dictionary)

with open("YourFilePath", "w") as f:
    f.write(json_dump)
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a dictionary and add key-value to it. I don't know the structure of the json but this can give an idea:

json_data = dict()
ht = 1
ft = 2

json_data["team1"] = {"ht": ht, "ft": ft}

print(json_data)
>>> {'team1': {'ht': 1, 'ft': 2}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.