我正在尝试提取和打印网址及其名称(在html文件中存在的<a href='url' title='smth'>NAME</a>
之间(保存在磁盘中),不使用使用BeautifulSoup或其他库。只是初学者的Python代码。
许愿的印刷格式是:
http://..filepath/filename.pdf
File's Name
so on...
我能够单独提取和打印所有网址或所有名称,但是我无法在标记之前的代码中附加一段时间后跟随的所有名称,并将其打印在每个网址下方。我的代码变得混乱,我很堆栈。 到目前为止,这是我的代码:
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()
如何让它发挥作用?我需要将它们组合成一个函数或def但是如何? PS。我在下面发布了一个答案,但我认为可能有更简单的Pythonic解决方案
答案 0 :(得分:0)
这是我需要的正确输出,但我相信有更好的方法。
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()