Django Web Scraping: Fetch Web Pages with Requests

You can use the requests library in Python to fetch web information in Django. Here is a simple example:

import requests

def get_webpage_content(url):
    response = requests.get(url)
    
    if response.status_code == 200:
        return response.text
    else:
        return None

# 调用方法并打印结果
url = 'http://example.com'
content = get_webpage_content(url)
print(content)

In this example, we have defined a function called get_webpage_content to fetch the content of a specified webpage and print it out. You can customize this function to suit your needs, such as adding exception handling, parsing HTML content, etc.

bannerAds