Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中读取特定文本和打印到文件之间的行_Python_Html_Parsing - Fatal编程技术网

在python中读取特定文本和打印到文件之间的行

在python中读取特定文本和打印到文件之间的行,python,html,parsing,Python,Html,Parsing,我是python新手,尝试将HTML源代码中两点之间的行提取到Output.txt。但是没有任何东西被写入输出文本文件,我也不知道为什么。任何帮助都将不胜感激 import urllib sock = urllib.urlopen('http://www.w3schools.com/xpath/xpath_examples.asp') htmlSource = sock.read() sock.close() text_file = open('/home/user/Desktop/Outpu

我是python新手,尝试将HTML源代码中两点之间的行提取到Output.txt。但是没有任何东西被写入输出文本文件,我也不知道为什么。任何帮助都将不胜感激

import urllib
sock = urllib.urlopen('http://www.w3schools.com/xpath/xpath_examples.asp')
htmlSource = sock.read()
sock.close()

text_file = open('/home/user/Desktop/Output.txt', 'w')

parsing=False
for line in htmlSource:
    if '<html lang="en-US">' in line:
        parsing = True
    elif '<script src="/bs/js/bootstrap.min.js"></script>' in line:
        parsing = False
    if parsing:
        text_file.write("%s\n" % line)

text_file.close()

您可以使用BeautifulSoup解析HTMLW您的最终目标是什么?我们将如何处理HTML页面的这一部分?htmlSource中的for行逐字读取htmlSource,至少在我尝试代码时是这样,而不是逐行读取。在htmlSource中,将for行下的整个主体替换为一个打印行,您将了解我的意思。附言:我同意@Hackaholic的说法:你可能想看看这个网站,你想把除了脚本标签以外的所有东西都写进文件,对吗??