所以,我的任務是編寫一個程序,讀取網頁的所有數據,並打印表格的所有超鏈接:查找超鏈接在Python 3
<a href="link">link text</a>
到目前爲止,我已經得到了這麼遠的幫助我的教練。
from urllib.request import urlopen
def findTitle(webpage):
encoding = "utf-8"
for webpagestr in webpage:
webpagestr = str(webpagestr,encoding)
if "<title>" in webpagestr:
indexstart = webpagestr.find("<title>")
indexend = webpagestr.find("</title>")
title = webpagestr[indexstart+7:indexend]
return title
return title
def H1headings(webpage):
encoding = "utf-8"
for webpagestr in webpage:
webpagestr = str(webpagestr,encoding)
if "<h1>" in webpagestr:
indexstart = webpagestr.find("<h1>")
indexend = webpagestr.find("</h1>")
heading = webpagestr[indexstart+4:indexend]
print(heading)
def main():
address = input("Enter URL to find title and more information: ")
try:
webpage = urlopen(address)
title = findTitle(webpage)
print("Title is", title)
H1headings(webpage)
webpage.close()
except Exception as exceptObj:
print("Error: ", str(exceptObj))
main()
當我運行這個程序,它可以讓我輸入一個URL,但它給了我後: 錯誤:局部變量「標題」分配
之前引用我不知道這意味着什麼。
後來,當我把我的嘗試之一:
def findTitle(webpage):
title = "Not Found"
encoding = "utf-8"
運行此程序時,它會給我:
Enter URL to find title and more information: http://jeremycowart.com
Title is not found
<a href="http://jeremycowart.com">Jeremy Cowart</a>
這就是我正在尋找,但我相信我假設有標題和標題以及鏈接文本。
我已經接近了,但我無法弄清楚。任何幫助,將不勝感激!