Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求在我的DJANGO项目中不起作用_Python_Python 3.x_Django_Django Views_Python Requests - Fatal编程技术网

Python 请求在我的DJANGO项目中不起作用

Python 请求在我的DJANGO项目中不起作用,python,python-3.x,django,django-views,python-requests,Python,Python 3.x,Django,Django Views,Python Requests,我在处理请求包时遇到问题,我已尽力解决此问题 问题-请求在我的Django项目中不起作用。它返回403禁止。但我的另一个Django项目(相同的笔记本电脑,具有相同的互联网连接,但不同的虚拟环境)也在使用相同的代码 因此,由于这个原因,我无法在现有的Django项目中使用requests包 下面是代码:这是演示代码,我将在两个Django项目和两个不同的虚拟环境上运行 import requests from bs4 import BeautifulSoup from django.shortc

我在处理请求包时遇到问题,我已尽力解决此问题

问题-请求在我的Django项目中不起作用。它返回403禁止。但我的另一个Django项目(相同的笔记本电脑,具有相同的互联网连接,但不同的虚拟环境)也在使用相同的代码

因此,由于这个原因,我无法在现有的Django项目中使用requests包

下面是代码:这是演示代码,我将在两个Django项目和两个不同的虚拟环境上运行

import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
​
# from .models import RequestData
​
​
def home(request):
    data = []
    urls = [
        "https://api.github.com/search/repositories",
        "https://www.dataquest.io",
        "https://www.python.org",
        "https://api.github.com/events"
    ]
    for url in urls:
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')
        if soup.title:
            data.append(str(soup.title))
        else:
            data.append("No Title")
    return render(request, "requester/index.html", {'data': data})

以下是我的HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {% for dat in data  %}
      <li>{{dat}}</li>
    {% endfor %}
</body>
</html>

文件
{数据%中的dat为%1}
  • {{dat}}
  • {%endfor%}
    以下是我对Django项目的回应

    第一个(有问题的)


    第二(没有任何问题)


    另外,我尝试了不同版本的requests包,但仍然存在相同的问题。

    您也使用相同的URL.py吗? 我可以看到不同之处(tst与测试)
    您可能正在调用另一个函数。

    不同环境中的不同结果或不同站点中的不同结果?不同虚拟环境中的不同django项目中的不同结果。但是在同一台机器上运行。是的,即使在url.py中也有相同的代码。这只是一个演示来说明我的问题。该函数的两个url.py代码没有区别。我没有调用其他函数,或者我没有传递任何其他参数。我的意思是,除了用于调用该函数的url之外,所有内容看起来都很好,请尝试导航到,而不是/tst/Sir,我为这两个项目创建了不同的url/tst和/或测试点指向同一代码。命名并不重要。我不是在刮这些网站。我只是通过使用这些网站来显示我的问题。