我要做的是找到一个网页的所有超链接这里是我到目前为止,但它不工作
from urllib.request import urlopen
def findHyperLinks(webpage):
link = "Not found"
encoding = "utf-8"
for webpagesline in webpage:
webpagesline = str(webpagesline, encoding)
if "<a href>" in webpagesline:
indexstart = webpagesline.find("<a href>")
indexend = webpagesline.find("</a>")
link = webpagesline[indexstart+7:indexend]
return link
return link
def main():
address = input("Please enter the adress of webpage to find the hyperlinks")
try:
webpage = urlopen(address)
link = findHyperLinks(webpage)
print("The hyperlinks are", link)
webpage.close()
except Exception as exceptObj:
print("Error:" , str(exceptObj))
main()
代码中存在多个问题。其中一个问题是,您正在尝试查找具有present、empty和唯一一个
href
属性:<a href>
的链接。你知道吗无论如何,如果您使用HTML解析器(好吧,解析HTML),事情会变得更加简单和可靠。使用^{} 的示例:
如果没有BeautifulSoap,您可以使用RegExp和simple函数。你知道吗
相关问题 更多 >
编程相关推荐