BeautifulSoup 또는 다른 라이브러리 를 사용 하지 않고 URL과 이름 ( <a href='url' title='smth'>NAME</a>
디스크에 저장된 html 파일) 을 추출하고 인쇄하려고합니다 . 초보자 용 Python 코드입니다. 원하는 인쇄 형식은 다음과 같습니다.
http://..filepath/filename.pdf
File's Name
so on...
모든 URL 또는 모든 이름을 단독으로 추출하여 인쇄 할 수 있었지만 태그 바로 앞에 포함 된 코드에서 잠시 후 뒤에 오는 모든 이름을 추가하고 각 URL 아래에 인쇄하지 못했습니다. 내 코드가 지저분 해지고 꽤 스택입니다. 지금까지 내 코드입니다.
import os
with open (os.path.expanduser('~/SomeFolder/page.html'),'r') as html:
txt = html.read()
# for urls
nolp = 0
urlarrow = []
while nolp == 0:
pos = txt.find("href")
if pos >= 0:
txtcount = len(txt)
txt = txt[pos:txtcount]
pos = txt.find('"')
txtcount = len(txt)
txt = txt[pos+1:txtcount]
pos = txt.find('"')
url = txt[0:pos]
if url.startswith("http") and url.endswith("pdf"):
urlarrow.append(url)
else:
nolp = 1
for item in urlarrow:
print(item)
#for names
almost identical code to the above
html.close()
어떻게 작동합니까? 나는 그것들을 하나의 기능 또는 정의로 통합해야하지만 어떻게? 추신. 아래에 답변을 게시했지만 더 간단하고 Pythonic 솔루션이있을 수 있다고 생각합니다.
이것은 내가 필요한 올바른 출력을 만들지 만 더 나은 방법이 있다고 확신합니다.
import os
with open ('~/SomeFolder/page.html'),'r') as html:
txt = html.read()
text = txt
#for urls
nolp = 0
urlarrow = []
while nolp == 0:
pos = txt.find("href")
if pos >= 0:
txtcount = len(txt)
txt = txt[pos:txtcount]
pos = txt.find('"')
txtcount = len(txt)
txt = txt[pos+1:txtcount]
pos = txt.find('"')
url = txt[0:pos]
if url.startswith("http") and url.endswith("pdf"):
urlarrow.append(url)
else:
nolp = 1
with open (os.path.expanduser('~/SomeFolder/page.html'),'r') as html:
text = html.read()
#for names
noloop = 0
namearrow = []
while noloop == 0:
posB = text.find("title")
if posB >= 0:
textcount = len(text)
text = text[posB:textcount]
posB = text.find('"')
textcount = len(text)
text = text[posB+19:textcount] #because string starts 19 chars after the posB
posB = text.find('</')
name = text[1:posB]
if text[0].startswith('>'):
namearrow.append(name)
else:
noloop = 1
fullarrow = []
for pair in zip(urlarrow, namearrow):
for item in pair:
fullarrow.append(item)
for instance in fullarrow:
print(instance)
html.close()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다