[英]Getting "An error occurred while calling o58.csv" error while writing a spark dataframe into a csv file
[英]Getting error while writing html page from website into CSV file
當我嘗試在 my_html.html 中編寫網頁的 html 時,會彈出此錯誤。 請指導我如何成功編寫它。
錯誤:文件“C:\\Users\\DRB\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\encodings\\cp1252.py”,第 19 行,編碼返回 codecs.charmap_encode(input,self.errors,encoding_table) [0] UnicodeEncodeError: 'charmap' codec can't encode character '\⇣' in position 84032: character maps to
import requests
def url_to_file(url, fname= "web_txt.html"):
response = requests.get(url)
html_text = response.text
if response.status_code == 200:
with open(fname, "w") as r:
r.write(str(html_text))
return html_text
return "Failed to perform its task."
url = "https://www.geeksforgeeks.org/absolute-relative-pathnames-unix/"
print(url_to_file(url))
嘗試以二進制模式打開頁面並保存響應的.content
,而不是.text
:
import requests
def url_to_file(url, fname="web_txt.html"):
response = requests.get(url)
html_content = response.content # <-- use .content
if response.status_code == 200:
with open(fname, "wb") as r: # <-- open file in binary mode
r.write(html_content)
return html_content.decode('utf-8', 'ignore') # <-- decode content as utf-8
return "Failed to perform its task."
url = "https://www.geeksforgeeks.org/absolute-relative-pathnames-unix/"
print(url_to_file(url))
印刷:
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" lang="en-US" prefix="og: http://ogp.me/ns#">
<![endif]-->
...<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" lang="en-US" prefix="og: http://ogp.me/ns#">
<![endif]-->
...
並保存web_txt.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.