网页抓取 Google Domains

我试图从前 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()



肥皂起泡泡
浏览 196回答 1
1回答

qq_笑_17

获得 100 个结果您必须逐页抓取,直到它有 100 个结果。假设 要废弃的关键字beautiful+girls URL 适用于像这样的第 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'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python