问题描述
我一直在创建具有多种用途的程序。我称之为直接收益电子数据库(EDDY)。我一直最困扰的一件事是EDDY的google搜索功能。 EDDY将要求用户提供输入。然后,EDDY将通过用加号('+')替换任何空格('')来稍微编辑输入,然后转到生成的url(无需打开浏览器)。然后,它会从网页中复制html,并应提供该结果和网站说明,并在没有HTML代码的情况下进行指定。
这是我到目前为止所拥有的。
import urllib
from urllib.request import urlopen,Request
from bs4 import BeautifulSoup
import requests
def cleanup(url):
html_content = requests.get(url).text
soup = BeautifulSoup(html_content,"lxml")
length = len(soup.prettify()) - 1
print(soup.prettify()[16800:length])
print(soup.title.text)
print(soup.body.text)
def eddysearch():
headers = {'User-Agent': 'Chrome.exe'}
reg_url = "http://www.google.com/search?q="
print("Ready for query")
query = input()
if(query != "quit"):
print("Searching for keyword: " + query)
print("Please wait...")
search = urllib.parse.quote_plus(query)
url = reg_url + search
req = Request(url=url,headers=headers)
html = urlopen(req).read()
cleanup(url)
eddysearch()
eddysearch()
有人可以帮我吗?预先感谢!
解决方法
h如果您不想使用SSL证书,则可以执行mockerGetUserProfile = func() UserProfile {
return fakeUserProfile
}
.read()
# Python 2.7.x
import urllib
url = "http://stackoverflow.com"
f = urllib.urlopen(url)
print f.read()