使用flask想实现一个简单的功能,
访问一个类似http://www.baidu.com的URL,然后获取URL的访问结果,展示在页面上,百度未果,新手求教!
怪我咯2017-04-18 09:48:37
Jump directly to the URL you want. Taking other people’s web pages is suspected of infringement. I don’t understand the purpose of this requirement. Want to make a mirror website?
黄舟2017-04-18 09:48:37
Don’t quite understand what you want to do? If you just want to access a simple web page, you can use urllib or something like that.
阿神2017-04-18 09:48:37
1. Write an input box on your page to obtain search keywords
2. Construct Baidu’s search url based on the value of your input
3. Splice urls
Example:
input value = test
The spliced url is as follows:
https://www.baidu.com/s?ie=ut...
巴扎黑2017-04-18 09:48:37
It is recommended to install and use the request library
$ pip install requests
from flask import make_response
import requests
@app.route("/test")
def test():
response = make_response()
# 请求百度url并取得响应内容
r = requests.get('http://www.baidu.com')
# 如要还原请求的headers
response.headers.update(r.headers)
# 输出请求百度得到的数据
response.data = r.text
return response
PHPz2017-04-18 09:48:37
import urllib2
response = urllib2.urlopen("http://www.baidu.com") # 传入一个URL,协议是HTTP协议
print response.read() # response对象有一个read方法,可以返回获取到的网页内容。
This blog has detailed instructions that you can read and make it easier to advance: http://cuiqingcai.com/947.html