[Python] 한글이 있는 xml 파싱 UnicodeEncodeError


한글이 있는 경우 파싱할 때 아래와 같은 에러가 나는 경우 처리 방법입니다.

Traceback (most recent call last):

  File "example.py", line 44, in <module>

    xmlR = xml.fromstring(r3.text)

  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1300, in XML

    parser.feed(text)

  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1640, in feed

    self._parser.Parse(data, 0)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 266-267: ordinal not in range(128)


encode('utf-8') 을 붙여주면 됩니다.

xmlR = xml.fromstring(r3.text.encode('utf-8'))


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

[CentOS] Python requests 모듈 설치


파이썬에서 url 을 불러와서 파싱을 하려면 requests 모듈이 사용되는데 기본적으로 설치가 되어 있지 않으므로 설치해 주어야 합니다.

# python tistory-rss.py

Traceback (most recent call last):

  File "example.py", line 2, in <module>

    import requests

ImportError: No module named requests


아래 처럼 설치하면 됩니다.

# yum install python-pip

# pip install requests


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

[티스토리] 오픈 API 로 블로그 목록 백업하기


예전엔 블록그 백업 기능을 지원했었는데 지금은 없어졌네요~ 유일하게 RSS 최대 50개까지 백업이 가능하지만 더 많은 포스팅이라면 Open API 를 이용할 수 밖에 없습니다.

백업하는 방법에 대해서만 언급하고 있으니 보시고 참고해서 프로그래밍은 직접하셔야 합니다.

인터넷에 찾아보시면 파이썬으로 되어 있는게 있더군요~


Open API 등록 : https://www.tistory.com/guide/api/manage/register


앱등록을 하면 앱관리에서 정보를 수정도 가능합니다.

[티스토리] 오픈 API 로 블로그 목록 백업하기

▶ 토큰 값 받기 ( 도움말 : https://tistory.github.io/document-tistory-apis/auth/authorization_code.html )

https://www.tistory.com/oauth/authorize?client_id={App ID}&redirect_uri={CallBack}&response_type=token

도움말을 보면 이해가 쉬울겁니다.

브라우저에서 위 주소를 치면 아래처럼 리다이렉트 되어 access_token 값이 날아옵니다.

모자이크 처리된 부분을 복사해서 아래 기능의 {access token} 값으로 이용하면 됩니다.

받고나서 1시간이 지나면 안된다는 군요~


▶ 블로그 정보 가져오기 ( 도움말 : https://tistory.github.io/document-tistory-apis/apis/v1/blog/list.html )

https://www.tistory.com/apis/blog/info?access_token={access token}&output=xml

output 은 json 또는 xml 둘 중에 하나입니다.


▶ 블로그 목록 가져오기 ( 도움말 : https://tistory.github.io/document-tistory-apis/apis/v1/post/list.html )

https://www.tistory.com/apis/post/list?access_token={access token}&output=xml&count=30&page=1&targetUrl=ivps

위에서 블로그 정보에서 총 게시글 수를 계산해서 page 와 count 프로그래밍 하시면 됩니다.

count 는 최대 30입니다.

targetUrl 은 티스토리 계정으로 총 5개의 블로그를 개설할 수 있는데 그 중에 하나를 입력하면 됩니다.


▶ 블로그 내용 가져오기 ( 도움말 : https://tistory.github.io/document-tistory-apis/apis/v1/post/read.html )

https://www.tistory.com/apis/post/read?access_token={access token}&output=xml&targetUrl=ivps&postId=3


이제 위의 3개의 API 를 조합해서 프로그래밍을 하면 됩니다.

나중에 여유가 되면 Python 으로 한번 만들어 보도록 하겠습니다.



▶ Python 으로 access_token 값 가져오기 : https://ivps.tistory.com/648

▶ Python 블로그 백업용 RSS 파일생성 : https://ivps.tistory.com/650



블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,