0
본적이 있습니다 these previous questions문제 깨끗한 텍스트 파일을 HTML에서 가져 오기
웹 사이트의 뉴스와 노트를 통합하려고합니다.
평판 뉴스 서비스 웹 사이트에서는 사용자가 의견 및보기를 게시 할 수 있습니다.
사용자 의견없이 뉴스 콘텐츠 만 얻으려고합니다. 나는 BeautifulSoup과 html2text으로 작업을 시도했다. 그러나 사용자 의견은 텍스트 파일에 포함됩니다. 심지어 위의 두 가지보다 유용한 진행 상황이없는 사용자 지정 프로그램 개발을 시도해 보았습니다.
아무도 진행 방법을 제공 할 수 있습니까?
코드 : 당신의 서식을 다음
import urllib
import urllib.request
myurl = "http://www.mysite.com"
sock = urllib.request.urlopen(myurl)
pagedata = str(sock.read())
sock.close()
file = open("output.txt","w")
file.write(pagedata)
file.close()
문자열의 많은 :
import urllib2
from bs4 import BeautifulSoup
URL ='http://www.example.com'
print 'Following: ',URL
print "Loading..."
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
identify_as = { 'User-Agent' : user_agent }
print "Reading URL:"+str(URL)
def process(URL,identify_as):
req = urllib2.Request(URL,data=None,headers=identify_as)
response = urllib2.urlopen(req)
_BSobj = BeautifulSoup(response).prettify(encoding='utf-8')
return _BSobj #return beauifulsoup object
print 'Processing URL...'
new_string = process(URL,identify_as).split()
print 'Buidiing requested Text'
tagB = ['<title>','<p>']
tagC = ['</title>','</p>']
reqText = []
for num in xrange(len(new_string)):
buffText = [] #initialize and reset
if new_string[num] in tagB:
tag = tagB.index(new_string[num])
while new_string[num] != tagC[tag]:
buffText.append(new_string[num])
num+=1
reqText.extend(buffText)
reqText= ''.join(reqText)
fileID = open('reqText.txt','w')
fileID.write(reqText)
fileID.close()
몇 가지 샘플을 제공하는 것이 좋습니다. – zhangyangyu