Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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
函数打开文件并从html Python中提取文本_Python_Html_Web Scraping_Beautifulsoup_Write - Fatal编程技术网

函数打开文件并从html Python中提取文本

函数打开文件并从html Python中提取文本,python,html,web-scraping,beautifulsoup,write,Python,Html,Web Scraping,Beautifulsoup,Write,我对Python非常陌生,我正在尝试编写一个程序来提取html标记(没有标记)中的文本,并将其写入另一个文本文件以供将来分析。我也提到了。我来是为了能得到下面的代码。但是我怎样才能把它作为一个单独的函数来写呢?差不多 "def read_list('file1.txt') 然后做同样的动作?我询问的原因是,此代码的输出(op1.txt)将用于词干分析,然后用于以后的另一个计算。这段代码的输出也不会像预期的那样逐行写入。非常感谢您的任何意见 f = open('file1.txt',

我对Python非常陌生,我正在尝试编写一个程序来提取html标记(没有标记)中的文本,并将其写入另一个文本文件以供将来分析。我也提到了。我来是为了能得到下面的代码。但是我怎样才能把它作为一个单独的函数来写呢?差不多

"def read_list('file1.txt')
然后做同样的动作?我询问的原因是,此代码的输出
(op1.txt)
将用于词干分析,然后用于以后的另一个计算。这段代码的输出也不会像预期的那样逐行写入。非常感谢您的任何意见

f = open('file1.txt', 'r')
for line in f:
    url = line
    html = urlopen(url)
    bs = BeautifulSoup(html, "html.parser")
    content = bs.find_all(['title','h1', 'h2','h3','h4','h5','h6','p'])

    with open('op1.txt', 'w', encoding='utf-8') as file:
        file.write(f'{content}\n\n')
        file.close()
像这样试试

from urllib.request import urlopen
from bs4 import BeautifulSoup

def read_list(fl):
    with open(fl, 'r') as f:
        for line in f:
            html = urlopen(line.strip()).read().decode("utf8")
            bs = BeautifulSoup(html, "html.parser")
            content = '\n'.join([x.text for x in bs.find_all(['title','p']+[f'h{n}' for n in range(1,7)])])
        
    with open('op1.txt', 'w', encoding='utf-8') as file:
        file.write(f'{content}\n\n')
像这样试试

from urllib.request import urlopen
from bs4 import BeautifulSoup

def read_list(fl):
    with open(fl, 'r') as f:
        for line in f:
            html = urlopen(line.strip()).read().decode("utf8")
            bs = BeautifulSoup(html, "html.parser")
            content = '\n'.join([x.text for x in bs.find_all(['title','p']+[f'h{n}' for n in range(1,7)])])
        
    with open('op1.txt', 'w', encoding='utf-8') as file:
        file.write(f'{content}\n\n')

file1.txt是包含URL列表的文件。一旦抓取完成,它应该被写入一个单独的文件(op1.txt)file1.txt是包含URL列表的文件。一旦刮削完成,它应该被写入一个单独的文件(op1.txt)谢谢,这就像一个魅力工程。但是你能解释一下你在这里做了什么吗?如果我想将此代码的输出用于程序的下一步,在下一步中,我必须将其与另一个文件进行比较并删除重复的文件,那么我是否应该将其作为一个不同的函数来执行,就在下面?我对Python非常陌生,仍然从一开始就在学习。任何反馈都已通知!谢谢你,这很有魅力。但是你能解释一下你在这里做了什么吗?如果我想将此代码的输出用于程序的下一步,在下一步中,我必须将其与另一个文件进行比较并删除重复的文件,那么我是否应该将其作为一个不同的函数来执行,就在下面?我对Python非常陌生,仍然从一开始就在学习。任何反馈都已通知!