Python 如何创建传递靓汤的函数

Python 如何创建传递靓汤的函数,python,python-3.x,machine-learning,beautifulsoup,Python,Python 3.x,Machine Learning,Beautifulsoup,我试图创建一个函数,将内容传递给Beauty soup,但我要么收到指示错误,要么它没有从提供的URL打印文本 from bs4 import BeautifulSoup import urllib.request def beautifulsoup(cont): url = "https://en.wikipedia.org/wiki/Machine_learning" content = urllib.request.urlopen(url) soup = Beautiful

我试图创建一个函数,将内容传递给Beauty soup,但我要么收到指示错误,要么它没有从提供的URL打印文本

from bs4 import BeautifulSoup
 import urllib.request
 def beautifulsoup(cont):
  url = "https://en.wikipedia.org/wiki/Machine_learning"
  content = urllib.request.urlopen(url)
  soup = BeautifulSoup(content)
print(soup.get_text())
我希望得到一个函数,将内容传递到beautiful soup并打印文本。

这应该可以

import requests
from bs4 import BeautifulSoup

def read_content():  
    url = "https://en.wikipedia.org/wiki/Machine_learning"
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html)
    return soup.text


print(read_content())
这应该行得通

import requests
from bs4 import BeautifulSoup

def read_content():  
    url = "https://en.wikipedia.org/wiki/Machine_learning"
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html)
    return soup.text


print(read_content())

谢谢,但希望它被定义为一个函数,你能帮我吗谢谢,但希望内容没有html标记,这在打印前有效(soup.get_text())你可以返回soup.text,我已经更改了答案谢谢,但希望它被定义为一个函数,你能帮我吗谢谢,但希望内容没有html标记,这在打印前有效(soup.get_text())你可以返回soup.text,我已经更改了答案