Python 3.x -:';的操作数类型不受支持;str';和';int';:在比较爬行网站上的基本域名和子域名时

Python 3.x -:';的操作数类型不受支持;str';和';int';:在比较爬行网站上的基本域名和子域名时,python-3.x,web-crawler,Python 3.x,Web Crawler,我试图通过计算网页中的内部链接总数与总链接数来找出特定网站的内部链接比率。如果比率>=0.5,它将返回-1,否则它将返回1 def get_domain_name(url): splitted = urlparse(url).hostname.split('.') return splitted[-2] + '.' + splitted[-1] def internal_link(url): icount = 0 count = 0 base_domain_

我试图通过计算网页中的内部链接总数与总链接数来找出特定网站的内部链接比率。如果比率>=0.5,它将返回-1,否则它将返回1

def get_domain_name(url):
   splitted = urlparse(url).hostname.split('.')
   return  splitted[-2] + '.' + splitted[-1]


def internal_link(url):
   icount = 0
   count = 0

   base_domain_name = get_domain_name(url)
   page = requests.get(url)
   soup = BeautifulSoup(page.content,'html.parser')
   href_links = soup.find_all('a',href=True)
   for link in href_links:
       count = count + 1
       child_domain_name = get_domain_name(link)
       if child_domain_name == base_domain_name:
           icount = icount + 1   

   if count != 0 :
       ilink_ratio = icount/count
        #elink_ratio = 1 - ilink_ratio
   else: 
       ilink_ratio = 0


   if ilink_ratio >= 0.5:
       return -1
   else:
       return 1


ans = internal_link('https://www.google.com')
print(ans)
我的预期输出将是1或-1,但我得到

ans = internal_link('https://www.google.com')

Traceback (most recent call last):

File "<ipython-input-76-aa8cd733497f>", line 1, in <module>
ans = internal_link('https://www.google.com')

File "<ipython-input-75-36aae0789101>", line 17, in internal_link
child_domain_name = get_domain_name(link)

File "<ipython-input-75-36aae0789101>", line 2, in get_domain_name
splitted = urlparse(url).hostname.split('.')

File "C:\Users\Dell\Anaconda3\lib\urllib\parse.py", line 367, in urlparse
url, scheme, _coerce_result = _coerce_args(url, scheme)

File "C:\Users\Dell\Anaconda3\lib\urllib\parse.py", line 123, in _coerce_args
return _decode_args(args) + (_encode_result,)

File "C:\Users\Dell\Anaconda3\lib\urllib\parse.py", line 107, in _decode_args
return tuple(x.decode(encoding, errors) if x else '' for x in args)

File "C:\Users\Dell\Anaconda3\lib\urllib\parse.py", line 107, in <genexpr>
return tuple(x.decode(encoding, errors) if x else '' for x in args)

File "C:\Users\Dell\Anaconda3\lib\site-packages\bs4\element.py", line 1181, in decode
indent_space = (' ' * (indent_level - 1))

TypeError: unsupported operand type(s) for -: 'str' and 'int'
ans=内部链路('https://www.google.com')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ans=内部链路('https://www.google.com')
文件“”,第17行,在内部链接中
子域名=获取域名(链接)
文件“”,第2行,位于get_域名中
splitted=urlparse(url).hostname.split('.'))
文件“C:\Users\Dell\Anaconda3\lib\urllib\parse.py”,第367行,在URLPRASE中
url,scheme,_-concure_-result=_-concure_-args(url,scheme)
文件“C:\Users\Dell\Anaconda3\lib\urllib\parse.py”,第123行,在强制参数中
返回_decode_args(args)+(_encode_result,)
文件“C:\Users\Dell\Anaconda3\lib\urllib\parse.py”,第107行,在解码参数中
返回元组(如果参数中的x为x,则x.decode(编码,错误)为x,否则为“”)
文件“C:\Users\Dell\Anaconda3\lib\urllib\parse.py”,第107行,在
返回元组(如果参数中的x为x,则x.decode(编码,错误)为x,否则为“”)
文件“C:\Users\Dell\Anaconda3\lib\site packages\bs4\element.py”,第1181行,解码
缩进空间=(“”*(缩进级别-1))
TypeError:-:“str”和“int”的操作数类型不受支持

您的
获取域名
功能无法处理所有的紧急情况

def get_domain_name(url):
   if url:
       print(url)
       hostname = urlparse(url).hostname
       if hostname is not None and '.' in hostname:
           splitted = hostname.split('.')
           return  splitted[-2] + '.' + splitted[-1]
这也许行得通。但你得找其他角落的案子。我只尝试过你在帖子中提到的url