我能够得到的网址在搜索页面使用下面的脚本
def get_source(url):
"""Return the source code for the provided URL.
Args:
url (string): URL of the page to scrape.
Returns:
response (object): HTTP response object from requests_html.
"""
try:
session = HTMLSession()
response = session.get(url)
return response
except requests.exceptions.RequestException as e:
print(e)
def scrape_google(query):
query = urllib.parse.quote_plus(query)
response = get_source("https://www.google.co.uk/search?q=" + query)
links = list(response.html.absolute_links)
google_domains = ('https://www.google.',
'https://google.',
'https://webcache.googleusercontent.',
'http://webcache.googleusercontent.',
'https://policies.google.',
'https://support.google.',
'https://maps.google.',
'https://play.google.')
https = ('https://')
for url in links[:]:
if url.startswith(google_domains):
links.remove(url)
return links
现在我想得到没有https的普通域名,www或类似下面的东西
wiki.org
itroasters.com
而且还需要删除任何重复。
有没有人可以帮助我得到预期的结果?
谢谢
2条答案
按热度按时间bwitn5fc1#
问题中没有解释删除netloc/path的'www.'前导的用例,可能是不明智的。
这里有一个模式,它将提供netloc/path的隔离,有或没有'www.'前导码。此代码还将处理没有方案的URL。需要删除的任何其他前缀都可以添加到PREFIXES列表中:
输出:
o2rvlv0m2#
要从URL中删除“https://"、“www."和任何其他子域,以便在Python 3中只获取域,可以使用urllib.parse模块。下面是一个例子:
这个脚本使用urllib.parse模块中的urlparse函数来解析URL。然后检索netloc属性,该属性包含域和子域信息。replace方法用于删除“www.”子域(如果存在)。唯一域存储在一个集合(unique_domains)中以消除重复。最后,将集合转换为列表(domain_list)以供打印。
请注意,此脚本假设URL格式良好,并遵循标准URL结构。它可能无法处理所有可能的变化或边缘情况。可能需要根据具体要求进行调整。