首页 > 解决方案 > Python:将 Beautifulsoup 写入文件 ERROR

问题描述

我的代码 我正在尝试使用 Selenium 获取页面源并将其传递给美丽的汤并将其连接到文件。

driver.get("https://www.amazon.ca/TP-Link-Dimmer-Switch-1-Pack-HS220/dp/B079775ZZQ/ref=sr_1_2?dchild=1&keywords=kasa+dimmer&qid=1615859475&sr=8-2")
driver.implicitly_wait(5)
page_html = driver.page_source
#print(page_html)
soup = BeautifulSoup(page_html, 'lxml')
#pageSource = driver.page_source
fileToWrite = open("soup.html", "w")
fileToWrite.write(str(soup))
fileToWrite.close()
driver.quit()

我收到一个错误

Traceback (most recent call last):
  File "c:/Users/T540p/OneDrive/PycharmProjects/.vscode/AmazonPriceTracker/selenium-scraper.py", line 32, in <module>
    fileToWrite.write(str(soup))
  File "C:\Users\T540p\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2009' in position 1017764: character maps to <undefined>

如果我使用 soup.encode('utf-8') 我明白了

  fileToWrite.write(soup.encode('utf-8'))
TypeError: write() argument must be str, not bytes

标签: pythonseleniumbeautifulsoup

解决方案


推荐阅读