Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do I extract specific lines from HTML files before and after a word?
#1
I am trying to extract the 10 lines before and after the word "apple" from a directory (with subdirectories) full of HTML files. I want to print out the lines into a CSV file. Ideally, the CSV file will contain two variables: 1) the HTML filename and 2) the 10 lines before and after the word "apple".

I have done the following:

import glob
import collections
import itertools
import sys
import csv

for filepath in glob.glob('**/*.html', recursive=True):
    with open(filepath) as f:
        before = collections.deque(maxlen=10)
        for line in f:
            if 'apple' in line:
                sys.stdout.writelines(before)
                sys.stdout.write(line)
                sys.stdout.writelines(itertools.islice(f, 10))
            break
        results = before.append(line)
        print(results)
I am currently getting a bunch of rows that say "None" in my terminal when I print the results. What is the issue here?
Quote
#2
Why do you expect that "append" method returns a value?
https://docs.python.org/2/library/collec...que.append
Nothing about the value returned. In case if a function doesn't return a result python always returns None.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Extract text between bold headlines from HTML CostasG 1 201 Aug-31-2019, 10:53 AM
Last Post: snippsat
  Getting a specific text inside an html with soup mathieugrimbert 9 1,412 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  [split] How to find a specific word in a webpage and How to count it. marpop 2 497 Mar-12-2019, 08:25 AM
Last Post: snippsat
  .txt return specific lines or strings s_o_what 8 764 Feb-08-2019, 11:49 AM
Last Post: snippsat
  BeautifulSoup4, How to get an HTML tag with specific class. Broadsworde 6 1,149 Nov-22-2018, 05:25 PM
Last Post: snippsat
  [Python 3] - Extract specific data from a web page using lxml module Takeshio 9 1,592 Aug-25-2018, 08:46 AM
Last Post: leotrubach
  Beautiful Soup - Delete All HTML - Except Specific Classes dj99 7 1,819 Jul-13-2018, 08:18 AM
Last Post: dj99
  webscraping - failing to extract specific text from data.gov rontar 2 696 May-19-2018, 08:01 AM
Last Post: rontar
  Extract contents from HTML chisox721 2 1,010 May-10-2018, 09:50 PM
Last Post: chisox721
  How to find a specific word in a webpage and How to count it. pratheep 11 19,531 Feb-08-2018, 04:07 PM
Last Post: pratheep

Forum Jump:


Users browsing this thread: 1 Guest(s)