2014-12-04 7 views
-2
import urllib.request 
import re  
text_file = open("scrape.txt","w") 
html = urllib.request.urlopen("http://en.wikipedia.org/wiki/Web_scraping") 
text = html.read() 
pattern = re.compile(b'<span dir="auto">(.+?)</span>') 
key = re.findall(pattern, text) 
text_file.write(key) 

그리고 그것의 반환이 오류 :웹 사이트에서 긁어 모으는 텍스트 파일에 데이터를 저장하는 방법은 무엇입니까?

Traceback (most recent call last): in text_file.write(key) TypeError: must be str, not list

+0

작동하지 않습니다. 이제 오류가 발생합니다 : Traceback (가장 최근의 마지막 호출) : text_file.write (found) TypeError : 바이트가 아닌 str 여야합니다. 어떻게 해결해야합니까 ?? – Anaya

답변

3

이 줄 :

key = re.findall(pattern, text) 

이 곳이 라인에서, 목록을 반환 :

text_file.write(key) 

당신이를 저장할 끈.

그래서 당신이 (아마) 원하는 것입니다 :

for found in key: 
    text_file.write(found) 
관련 문제