如何从特定 url 获取文本?

How to get text from specific url?

我想知道是否有任何方法可以使用 python 从某些 url 获取文本。

例如从这一个https://www.ixbt.com/news/2022/04/20/160-radeon-rx-6400.html

提前致谢。

您可以在 python 中使用 BeautifulSoup:

进行网页抓取
from urllib.request import urlopen
from bs4 import BeautifulSoup

url = "https://www.ixbt.com/news/2022/04/20/160-radeon-rx-6400.html"
html = urlopen(url).read()
soup = BeautifulSoup(html, features="html.parser")

text = soup.get_text()

之后您可以将提取的文本保存到文本文件中:

text_file = open("webscrap.txt", "w", encoding="utf-8")
text_file.write(text)
text_file.close()