我想对网页应用字符串操作,就像我逐行处理本地普通文件一样:
save = []
ins = open("my_file.html", "r")
for line in ins:
if "/html/" in line and "thumbs" in line:
print(line)
但是,当我尝试直接获取网页然后将其转换为 utf-8 时,我无法再逐行解析,这是我的代码:
fp = urllib.request.urlopen(base + ".html")
mystr = fp.read()
mystr = mystr.decode("utf-8")
for line in mystr:
if "/html/" in line and "thumbs" in line:
print(line)
那么我在这里做错了什么,这是我在收到页面后解码页面的方式,我正在使用的库,我使用字符串或其他东西的方式?
这是cat my_file.html | 的结果头
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-4477008-1']);
_gaq.push(['_trackPageview']);
(function() {
最佳答案
So whats am I doing wrong here
迭代文件对象会给出文件的行,但迭代字符串会给出单个字符(作为长度为 1 的字符串)。
您需要自己将字符串拆分回行,例如与.splitlines()
。
关于python - 在python中逐行读取html url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529361/