网页搜集谷歌域名

问题描述 投票:0回答:1

我想从前100个结果中获取域名列表:

例如:abc.com/xxxx/dddd域名应为:abc.com

我使用以下代码:

import time
from bs4 import BeautifulSoup
import requests
search=input("What do you want to ask: ")
search=search.replace(" ","+")
link="https://www.google.com/search?q="+search
print(link)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
source=requests.get(link, headers=headers).text
soup=BeautifulSoup(source,"html.parser")

soup=BeautifulSoup(source,"html.parser")

但是,我不知道如何仅选择域,也不知道如何指定100个结果。

当我写soup.text我只得到:

'te - Pesquisa Google(function(){window.google={kEI:\'jsCaXM3AHM6g5OUP4eyT2A0\',kEXPI:\'31\',authuser:0,kscs:\'c9c918f0_jsCaXM3AHM6g5OUP4eyT2A0\',kGL:\'BR\'};google.sn=\'web\';google.kHL=\'pt-BR\';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.time=function()
python web-scraping
1个回答
2
投票

获得100个结果

您必须在每个页面上废弃,直到它有100个结果。假设关键字beautiful + girls网址为第2页,就像这个https://www.google.com/search?q=beautiful+girls&start=10一样

仅获取域名

首先,您必须使用类'srg'获取所有div(在查看源代码之后,我看到所有链接都在此处)

srg_divs = soup.findAll("div", {"class": "srg"})

然后你会找到所有的标签

out = ''
for div in srg_divs:
    links = div.find_all('a', href=True)
    for a in links:
        # url to domain
        parsed_uri = urlparse(a['href'])
        domain = '{uri.netloc}'.format(uri=parsed_uri)
        # exclude googleusercontent.com
        if 'googleusercontent' in domain or domain == '':
            continue
        out += domain + '\n'
© www.soinside.com 2019 - 2024. All rights reserved.