先生们,请帮助,我写了这个东西:
import re
from wiki_req import get_topic_page
def get_topic_text (topic):
html_content = get_topic_page(topic)
words = re.findall("[а-яА-Я\-]+", html_content)
text = " ".join(words)
return text
print(get_topic_text ("Москва"))
他给了我这个
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/helloworld/ООП/ООП4/index.py", line 10, in <module>
print(get_topic_text ("Москва"))
File "C:/Users/admin/PycharmProjects/helloworld/ООП/ООП4/index.py", line 6, in get_topic_text
words = re.findall("[а-яА-Я\-]+", html_content)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\re.py", line 223, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
请帮帮我
这是原始的 wiki_req 文件本身
from requests import get
"""https://ru.wikipedia.org/wiki/Россия"""
def get_link(topic):
link = "https://ru.wikipedia.org/wiki/" + topic
return link
def get_topic_page (topic):
link = get_link(topic)
html_content = get(link).text
with open("new.html", "w", encoding="utf-8") as f:
f.write(html_content)
return True
print(get_topic_page ("биржа"))
get_topic_page
模块中的函数wiki_req
返回类型为 的值bool
,在您的情况下为True
。您正在尝试将该值
True
用作正则表达式搜索的字符串(或文件),这就是发生错误的原因。您的替代价值声明没有意义,请参阅:
最有可能的是,错误只是在模块
wiki_req
中,您应该在其中返回HTML
-code,而不是布尔值:另一种解决方案是从函数
get_topic_page
写入结果的文件中读取: