구문 분석 결과를 텍스트 파일로 저장해야합니다. outfile.write에서 ("n \"항목 +) 형식 오류를 파일 "c.py", 라인 26 : 역 추적 (마지막으로 가장 최근 통화) :html 파일을 구문 분석 한 후 튜플을 문자열로 변환하십시오.
import urllib
from bs4 import BeautifulSoup
import urlparse
path = 'A html file saved on desktop'
f = open(path,"r")
if f.mode == 'r':
contents = f.read()
soup = BeautifulSoup(contents)
search = soup.findAll('div',attrs={'class':'mf_oH mf_nobr mf_pRel'})
searchtext = str(search)
soup1 = BeautifulSoup(searchtext)
urls = []
for tag in soup1.findAll('a', href = True):
raw_url = tag['href'][:-7]
url = urlparse.urlparse(raw_url)
urls.append(url)
print url.path
with open("1.txt", "w+") as outfile:
for item in urls:
outfile.write(item + "\n")
그러나, 나는이를 얻을 수 튜플 ("str"이 아님) 만 튜플에 연결하십시오.
튜플을 문자열로 변환하고 텍스트 파일로 저장할 수 있습니까? 감사.
'print (item)'을 시도하면 문자열이 아닌 튜플이 표시됩니다. 문자열은 함께 추가 할 수 있습니다. –