我正在从Wiki页面提取url链接,当它试图解析某些链接时,出现了一个“ValueError”。我正在寻找一种方法来忽略错误或修复问题。似乎当循环提取链接时,它会遇到它无法识别为链接的链接并进行回溯
from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error
import ssl
import re
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input("Enter First Link: ")
if len(url)<1: url = "https://www.bing.com/search?q=k+means+wiki&src=IE-SearchBox&FORM=IENAD2"
position = 18
process = 7
#to repeat 18 times#
for i in range(process):
html = urllib.request.urlopen(url, context=ctx)
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')
count = 0
for tag in tags:
count = count +1
#make it stop at position 3#
if count>position:
break
url = tag.get('href', None)
print(url)
加薪:
ValueError Traceback (most recent call last)
ValueError: unknown url type: '/search?q=Cluster+analysis%20wikipedia&FORM=WIKIRE'
出现错误的原因是该链接无效。您可以尝试将“https://bing.com”前置到URL的开头,或者捕获错误
要捕获错误:
要预先设置URL:
https://docs.python.org/3/tutorial/errors.html#errors-and-exceptions
有关错误和异常,请参阅python文档
你可以把它放在循环中:
希望有帮助
它遇到的url没有架构或域。它是一个相对的url,意味着需要将它附加到当前页面url中才能转到它。URL通常以schema://domain.domain like 在
https://www.facebook.com
中。如果您检查您的网址,以确保它们包含架构和域,然后附加它们,如果他们丢失,那么您将避免这个错误举个例子:
/search?q=stack+overflow
可能是在google上搜索堆栈溢出的相对url
要重建完整的url,只需在开头添加
https://www.google.com
,它就变成了一个实际的搜索链接https://www.google.com/search?q=stack+overflow
相关问题 更多 >
编程相关推荐