我试图从前 100 个结果中获取域列表:例如:abc.com/xxxx/dddd 域应该是:abc.com我正在使用以下代码:import timefrom bs4 import BeautifulSoupimport requestssearch=input("What do you want to ask: ")search=search.replace(" ","+")link="https://www.google.com/search?q="+searchprint(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).textsoup=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()
1 回答

qq_笑_17
TA贡献1818条经验 获得超7个赞
获得 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'
添加回答
举报
0/150
提交
取消