Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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中查找可见HTML中的文本_Python_Html - Fatal编程技术网

在python中查找可见HTML中的文本

在python中查找可见HTML中的文本,python,html,Python,Html,我正在努力做到以下几点: 我有一个文本文件,其中逐行包含一些值 根据页码生成值列表的网站。以下示例中的值为XXX和YYY python脚本读取第一个文本文件(使用一个集合高效地进行0(1)查找)并在网站页面中按+1逐页搜索,如果发现值匹配,则必须打印页码 搜索必须类似于www.site.com/1 www.site.com/2 www.site.com/3…等 HTML来源: <pre class="values"> <strong>A</strong>

我正在努力做到以下几点:

  • 我有一个文本文件,其中逐行包含一些值
  • 根据页码生成值列表的网站。以下示例中的值为XXX和YYY
  • python脚本读取第一个文本文件(使用一个集合高效地进行0(1)查找)并在网站页面中按+1逐页搜索,如果发现值匹配,则必须打印页码
  • 搜索必须类似于www.site.com/1 www.site.com/2 www.site.com/3…等

    HTML来源:

    <pre class="values">
        <strong>A</strong>
        <strong>B</strong>
        <strong>C</strong>
        <span id="1">
            <a href="/#">+</a> 
            <span title="1">1</span>
            <a href="/#">XXX</a>
            <a href="/#">YYY</a>
        </span>
    </pre>
    
    从xml.etree导入ElementTree作为ET
    A
    B
    C
    1.
    将open('/path/to/file.html')作为fp:
    html=ET.fromstring(fp.read())
    对于html.iter()中的节点:
    如果node.tag==“a”:
    打印node.text
    
    请你再解释一下好吗。也许我可以破解一些代码。html代码不能添加到python代码中。python代码必须请求(解析)网页,并搜索.txt文件中的任何行是否与网页上的任何内容匹配。快乐的黑客!对于所提供的HTML源,您希望的输出是什么?我希望的输出是XXX和YYY,如果.txt文件中有匹配的内容。
    with open("values.txt", "r") as f1:
            lines = set(f1) # efficient 0(1) lookups using a set
            for line in HTML :
                if line in lines:
                    print(line)
    
    from xml.etree import ElementTree as ET
    
    <pre class="values">
        <strong>A</strong>
        <strong>B</strong>
        <strong>C</strong>
        <span id="1">
            <a href="/#">+</a> 
            <span title="1">1</span>
            <a href="/#">XXX</a> <a href="/#">YYY</a>
        </span>
    </pre>
    
    with open('/path/to/file.html') as fp:
        html = ET.fromstring(fp.read())
    
    for node in html.iter():
        if node.tag == 'a':
            print node.text