使用Python在Google中搜索 [英] Searching in Google with Python
问题描述
我想使用python脚本在Google中搜索文本,并返回每个结果的名称,描述和URL.我目前正在使用此代码:
I want to search a text in Google using a python script and return the name, description and URL for each result. I'm currently using this code:
from google import search
ip=raw_input("What would you like to search for? ")
for url in search(ip, stop=20):
print(url)
这仅返回URL.如何返回每个URL的名称和描述?
This returns only the URL's. How can I return the name and description for each URL?
推荐答案
虽然还算不错,但我发现自己现在是一个不错的解决方案(如果可以做得更好,我可以对其进行编辑).我像在Google中一样搜索(仅返回URL)和Beautiful Soup软件包结合在一起来解析HTML页面:
Not exatcly what I was looking for, but I found myself a nice solution for now (I might edit this if I will able to make this better). I combined searching in Google like I did (returning only URL) and the Beautiful Soup package for parsing HTML pages:
from google import search
import urllib
from bs4 import BeautifulSoup
def google_scrape(url):
thepage = urllib.urlopen(url)
soup = BeautifulSoup(thepage, "html.parser")
return soup.title.text
i = 1
query = 'search this'
for url in search(query, stop=10):
a = google_scrape(url)
print str(i) + ". " + a
print url
print " "
i += 1
这给了我页面标题和链接的列表.
This gives me a list of the title of pages and the link.
还有另一个很棒的解决方案:
And another great solutions:
from google import search
import requests
for url in search(ip, stop=10):
r = requests.get(url)
title = everything_between(r.text, '<title>', '</title>')
这篇关于使用Python在Google中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!