python - flask获取URL访问结果
PHP中文网
PHP中文网 2017-04-18 09:46:37
0
5
652

使用flask想实现一个简单的功能,
访问一个类似http://www.baidu.com的URL,然后获取URL的访问结果,展示在页面上,百度未果,新手求教!

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
刘奇

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?

黄舟

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.

阿神

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...

巴扎黑

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
PHPzhong
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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!