Write cleaned BS4 data to csv file

陌路散爱 提交于 2020-03-21 11:04:12

问题


from selenium import webdriver

from bs4 import BeautifulSoup

import csv

chrome_path = r"C:\Users\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)

driver.get('http://www.yell.com')

search = driver.find_element_by_id("search_keyword")

search.send_keys("plumbers")

place = driver.find_element_by_id("search_location")

place.send_keys("London")

driver.find_element_by_xpath("""//*[@id="searchBoxForm"]/fieldset/div[1]/div[3]/button""").click()

soup = BeautifulSoup(driver.page_source, 'html.parser')

for names in soup.find_all("span", {"class": "businessCapsule--name"}):
    print(names.text)

Output = soup.find_all("span", {"class": "businessCapsule--name"})

with open('comple16.csv', 'w') as csv_file:
    csv.register_dialect('custom', delimiter='\n', quoting=csv.QUOTE_NONE, escapechar='\\')
    writer = csv.writer(csv_file, 'custom')
    row = Output
    writer.writerow(row)

Currently the code is producing this in the csv file = class": "businessCapsule-- (scraped text)

I would like to only print the scraped text into the CSV file (without the tags)

Please help.


回答1:


from selenium import webdriver

from bs4 import BeautifulSoup`

import csv

chrome_path = r"C:\Users\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)

driver.get('http://www.yell.com')

search = driver.find_element_by_id("search_keyword")

search.send_keys("plumbers")

place = driver.find_element_by_id("search_location")

place.send_keys("London")

driver.find_element_by_xpath("""//*[@id="searchBoxForm"]/fieldset/div[1]/div[3]/button""").click()

soup = BeautifulSoup(driver.page_source, 'html.parser')

Output = []
for names in soup.find_all("span", {"class": "businessCapsule--name"}):
    Output.append(names.text)

with open('comple16.csv', 'w') as csv_file:
    csv.register_dialect('custom', delimiter='\n', quoting=csv.QUOTE_NONE, escapechar='\\')
    writer = csv.writer(csv_file, 'custom')
    row = Output
    writer.writerow(row)



回答2:


After:

Output = soup.find_all("span", {"class": "businessCapsule--name"})

add:

Output = [row.text for row in Output]

in order to extract text from SPAN fields.



来源:https://stackoverflow.com/questions/60287217/write-cleaned-bs4-data-to-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!