使用 Python 访问本地保存在计算机上的 HTML 页面 [英] Accessing an HTML page saved locally on computer in Python

查看:94
本文介绍了使用 Python 访问本地保存在计算机上的 HTML 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地目录中保存了以下 HTML 页面路径:C:\Users\Sony\Desktop\b.html".现在我想在这个页面中搜索一个词:

I have the following path of HTML page saved in local directory: "C:\Users\Sony\Desktop\b.html". Now I want to search for a word in this page:

url = r"C:\Users\Sony\Desktop\b.html"
page = open(url)

我想把它写成:

k="C:\Users\Sony\Desktop\b.html"
url = r k
page = open(url)

它给了我错误,在这种情况下如何应用读取模式 r 因为如果我写 url=r"k" k> 不会被当作变量而是字符串.

It is giving me error, how to apply read mode r in this case because if I write url=r"k" the k won't be taken as a variable but a string.

我收到此错误:

IOError: [Errno 22] 无效模式 ('r') 或文件名:'C:\Users\Sony\Desktop\x08.html'

IOError: [Errno 22] invalid mode ('r') or filename: 'C:\Users\Sony\Desktop\x08.html'

推荐答案

原始字符串前缀仅适用于文字.字符串已经不可修复地损坏了,因为不检查就不可能单方面确定原件应该是 r'\b' 还是 r'\x08'.您所能做的就是执行替换操作,并希望您选择正确的操作.

The raw string prefix only applies to literals. The string is already irreparably damaged, since it is impossible to unilaterally determine without inspection whether the original was supposed to be r'\b' or r'\x08'. All you can do is perform a replace operation and hope you pick the right one.

k = k.replace('\x08', r'\x08')

这篇关于使用 Python 访问本地保存在计算机上的 HTML 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆