如何创建一个传递漂亮汤的功能

时间:2019-10-09 13:25:23

标签: python python-3.x machine-learning beautifulsoup

我正在尝试创建一个将内容传递给漂亮汤的函数,但是出现指示错误或者它无法从提供的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())

我希望获得一个将内容传递到漂亮汤中并打印出文字的功能。

1 个答案:

答案 0 :(得分:2)

这应该有效

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())