#coding:gbk
from selenium import webdriver
from selenium.webdriver.common import options
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path ="chromedriver",options=options)
driver.get("https://liquicity.com/artists/")
time.sleep(1)
num=-1
while True:
try:
num+=1
artists=driver.find_elements(By.CLASS_NAME,'elementor-button-text')[num].text
print(artists)
with open("liquicity_artists.txt","a") as f:
if num == 22 :
f.write("Ella Noel\n")
else:
f.write(artists+"\n")
except:
break
先看看是哪一行有Unicode字符,发现是第22行。那么写一个if判断到22行时就输出修改过的字母追加到文本就行。

with open("liquicity_artists.txt","a") as f:
if num == 22 :
f.write("Ella Noel\n")
else:
f.write(artists+"\n")
这里的open用的a参数,这样就可以追加文本,而不是替换文本之前的内容。
结果正常导出到txt,编码也是ANSI码。
不过这种方法适合于我这种爬取数据少的情况下,其实更简单直接复制控制台里面的输出的内容过去到文本里面改也可以。
参考文献:
python selenium while 循环
该篇博客介绍了一个Python爬虫程序,利用Selenium库动态加载网页内容。程序逐页抓取 Liquicity网站上的艺术家名字,并将结果保存到TXT文件中。在遇到Unicode字符时,特别处理了第22行的数据,将其改为'EllaNoel'。博客还提到,对于少量数据,直接复制控制台输出到文本进行编辑也是一种可行的方法。

2万+

被折叠的 条评论
为什么被折叠?



