나는 html 문자열을 만들고 .html 파일에 쓰려고합니다. json과 xml이있는 웹 사이트에서 데이터를 가져옵니다. 이렇게하면 텍스트와 링크를 가져와 삼중 따옴표로 묶은 문자열에 배치합니다. 이 html 문자열에 더 많은 것이 있지만이 정도만 문제를 보여줄 것입니다. 뉴스 문자열을 받아 .html 파일에 문자열을 쓰는 함수가 있습니다. 나는 데이터를 삽입하는 모든 기능을 작동하기 전에 문자열을 파일에 쓰기 기능에 보내기 전에 화면에 인쇄하고 텍스트 파일에 복사 한 다음 브라우저에 표시했기 때문에 올바른 모든 문자가 올바르게 표시되기 때문에 작동한다는 것을 알고 있습니다. 데이터. 이 문제는 파이썬이 파일에 쓸 때 발생합니다. 나는 "문자열 오류가 있어야한다"고합니다.Python, html 및 텍스트 파일에 쓰기
유일한 데이터가 텍스트 일 때 파일에 쓰기가되었지만 일단 URL이되기 시작하면 쓰기가 실패합니다. 글쎄, 매번 글을 쓰지는 않았지만 한번이면 충분합니다.
나는 이론을 가지고있다. 일부 URL에는 이스케이프 문자가 포함되어 있거나 너무 길게 프로그래밍되지 않았습니다. 또는 삽입 할 데이터를 html 문자열에 삽입하기 전에 트리플 따옴표로 묶어야합니다. 그게 가능 할지도 모르겠다.
뉴스 : % (데이터, 데이터, 데이터)를 사용하여 데이터를 삽입하려고했지만 문제가 해결되지 않았습니다.
news = ("""<div id='weather'>""" +
"""<center><h1>Weather</h1></center>""" +
"""<center><cite>Weatherunderground</cite></center>""" +
"""<hr />""" +
"""<h6>""" + weather['forecast']['txt_forecast']['forecastday'][0]['title'] + """</h6>""" +
"""<p>""" + weather['forecast']['txt_forecast']['forecastday'][0]['fcttext'] + """</p>""" +
"""<h6>Forecast</h6>"""
"""<table border='1'>"""
"""<tr>"""
"""<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['date']['weekday'] + """</th>""" +
"""<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][1]['date']['weekday'] + """</th>""" +
"""<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][2]['date']['weekday'] + """</th>""" +
"""<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][3]['date']['weekday'] + """</th>""" +
"""</tr>""" +
"""<tr>""" +
"""<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" +
"""<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" +
"""<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" +
"""<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" +
"""</tr>""" +
"""<tr>""" +
"""<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" +
"""</tr>""" +
"""<tr>""" +
"""<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" +
"""<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" +
"""</tr>""" +
"""</table>""" +
"""</div>""" +
"""<hr />""" +
"""<div id='kindle'>""" +
"""<center><h1>Amazon Daily Kindle Deal</h1></center>""" +
"""<center><cite>amazon</cite></center>""" +
"""<hr />""" +
"""<div class='firstHeadline'>""" +
"""<h3>""" + kind[0][0] + """</h3>""" +
"""<p class='content'>""" + kind[1][0] + """</p>""" +
"""<p class='url'><a href='""" + kind[2][0] + """'>""" + kind[2][0] + """</a></p>""" +
"""</div>""" +
"""<hr />""" +
"""</div>""" +
"""<div id='amazonApp'>""" +
"""<center><h1>Amazon App of the Day</h1></center>""" +
"""<center><cite>amazon</cite></center>""" +
"""<hr />""" +
"""<div class='firstHeadline'>""" +
"""<h3>""" + app[0][0] + """</h3>""" +
"""<p class='content'>""" + app[1][0] + """</p>""" +
"""<p class='url'><a href='""" + app[2][0] + """'>""" + app[2][0] + """</a></p>""" +
"""</div>""" +
"""<hr />""" +
"""</div>""")
오류를 찾으셨습니까? 그렇다면 게시 할 수 있습니까? – Blender
오류가 발생합니다 : UnicodeEncodeError : 'ascii'코덱이 3471 위치의 문자 '\ xbf'를 인코딩 할 수 없습니다. 서수가 범위에 없음 (128) – Derek
Sigh. 이것은 FAQ입니다. "UnicodeEncodeError ascii codec"을 검색하십시오. 읽기 : http://docs.python.org/howto/unicode.html –