Python怎么把txt文件里的内容提取出来?

import requests
from bs4 import BeautifulSoup

f = open('114514.txt', 'r', encoding='UTF-8')
hhh = f.read()
f.close()

science_url = hh

我是这样弄的,当是science_url = hh这一步出现了问题

你上边是hhh
下边是hh
改成一样的就好

错误提示是你请求的url不正确,缺少schema,即http://或https://,检查hh,获取的链接,用字符串拼接方式加上schema。

前面变量不是 hhh 么? 你后面这个hh 是哪来的, 你这个就是读文件操作吧

import requests
from bs4 import BeautifulSoup


f = open('114514.txt', 'r', encoding='UTF-8')
hh = f.read()
f.close()

def get_html_text(url):
    r = requests.get(url)
    if r.status_code == 200:
        r.encoding = 'UTF-8'
        return r.text
    else:
        return '爬取失败!'

science_url = hh
html = get_html_text(science_url)
soup = BeautifulSoup(html, 'html.parser')
div_tag = soup.find('div',attrs={'class':'center_bottom'})
a_tag = div_tag.find('a')
text = a_tag.get_text()
print(text)

源代码是这样的