python - 将清理后的 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": "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)
目前代码在 csv file = class": "businessCapsule-- (scraped text) 中产生这个
我只想将抓取的文本打印到 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')
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)
推荐阅读
- c# - DataContext.Refresh 需要很长时间
- python - Spacy 基于令牌的匹配与令牌之间的“n”个令牌
- php - 条件作为 PHP 输出缓冲区中的字符串
- amazon-web-services - 在 AWS Lambda 函数上运行 aws S3 cli 命令时令牌无效
- angular - 如何在 TypeScript (Angular) 中读取 Json 文件?
- mysql - SQL查询以获取特定用户的所有数据
- javascript - javascript中连接4的Minimax实现问题
- azure - 具有多个数据库的 Azure SQL 数据仓库
- python - Groupby 然后检查行匹配并计算该值的并发实例
- java - Apache POI - 基于 2 个日期参数导出到 Excel