我想在python脚本中通过Google搜索找到“Spotlight 29 casino address”的地址。为什么我的代码不能正常工作?在
from bs4 import BeautifulSoup
# from googlesearch import search
import urllib.request
import datetime
article='spotlight 29 casino address'
url1 ='https://www.google.co.in/#q='+article
content1 = urllib.request.urlopen(url1)
soup1 = BeautifulSoup(content1,'lxml')
#print(soup1.prettify())
div1 = soup1.find('div', {'class':'Z0LcW'}) #get the div where it's located
# print (datetime.datetime.now(), 'street address: ' , div1.text)
print (div1)
如果你想得到谷歌搜索结果。Selenium with Python是更简单的方法。在
下面是简单的代码。在
Google使用javascript呈现来达到这个目的,这就是为什么你不能用urllib.request.urlopen. 在
作为解决方案,您可以使用selenium-python库来模拟浏览器。使用“pip Install selenium”控制台命令进行安装,然后这样的代码可以工作:
相关问题 更多 >
编程相关推荐