将已清理的BS4数据写入csv文件
问题描述:
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": "busine*sule--name"}):
print(names.text)
Output = soup.find_all("span", {"class": "busine*sule--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)
当前代码正在csv文件= class中生成此代码:" busine*sule-(草稿文字)
Currently the code is producing this in the csv file = class": "busine*sule-- (scraped text)
我只想将抓取的文本打印到CSV文件中(不带标签)
I would like to only print the scraped text into the CSV file (without the tags)
请帮助.
答
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": "busine*sule--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)