Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to Find & Count String Patterns Between two Markers in a HTML file
#1
Hello,

I have a HTML file from which I am trying to extract strings so that I can count them and then hopefully export the analysis of those results in the form of a table.

My HTML file has many "<div></div>" tags that house a repetitive unique pattern. The pattern is:

" to [Name of Player], "

The only thing that is standard in the above are the blank spaces before and after the 'to' along with the comma and space at the end. I need a way to get the full player name from the above pattern based on my HTML file.


The HTML File is as below

</div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.6</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Archer to Fakhar Zaman, no run, wide ball prompts a rash swing from Fakhar. Bat nowhere near ball. Better over from England</div>
                                                </div><!-- react-text: 3750 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.5</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Archer to Fakhar Zaman, no run, beats the inside edge this time. Bit of swing for Archer. This has been a challenging over</div>
                                                </div><!-- react-text: 3758 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item pre ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.4</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Archer to Fakhar Zaman, no run, beautiful bowling. Good length, at pace and swinging away from Imam's outside edge</div>
                                                </div>
                                                <div>
                                                    <p class="comment">"Pakistan could learn a lot from the fearless approach of the Bangladeshi batsmen", Nathan Barnes says. "Will they have the courage to take a risk? "</p>
                                                </div>
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.3</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Archer to Fakhar Zaman, no run, shorter and wider, left alone this time</div>
                                                </div><!-- react-text: 3775 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.2</div>
                                                        <div class="over-circle low-score"><span class="over-score">4lb</span></div>
                                                    </div>
                                                    <div class="description">Archer to Fakhar Zaman, 4 leg byes, too straight, almost legsid-ish, and Fakhar flicks it off the hips to the fine leg boundary. This is a <strong>good start for Pakistan</strong></div>
                                                </div><!-- react-text: 3783 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">1.1</div>
                                                        <div class="over-circle low-score"><span class="over-score">1</span></div>
                                                    </div>
                                                    <div class="description">Archer to Imam-ul-Haq, 1 run, immediate threat from Archer. Straighter, squared Imam up, an outside edge trickles through to third man</div>
                                                </div><!-- react-text: 3791 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item end-of-over">
                                                <h4>
                                                    <!-- react-text: 3794 -->END OF OVER:
                                                    <!-- /react-text -->
                                                    <div>
                                                        <!-- react-text: 3796 -->1 | 9 Runs | PAK: 9/0
                                                        <!-- /react-text -->
                                                        <!-- react-text: 3797 -->| RR: 9.00
                                                        <!-- /react-text -->
                                                    </div>
                                                </h4>
                                                <ul class="two-col-table">
                                                    <li>Fakhar Zaman<span>8 (4b)</span></li>
                                                    <li>Imam-ul-Haq<span>1 (2b)</span></li>
                                                </ul>
                                                <ul class="two-col-table">
                                                    <li>Chris Woakes<span>1-0-9-0</span></li>
                                                </ul>
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.6</div>
                                                        <div class="over-circle high-score"><span class="over-score">4</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Fakhar Zaman, <b>FOUR</b> runs, <strong>slashed away</strong> by Zaman. Got just a bit more room, fuller delivery angling away from the left-hander, and a slice gets it just wide of the man at point.</div>
                                                </div><!-- react-text: 3810 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.5</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Fakhar Zaman, no run, driven to mid-off</div>
                                                </div><!-- react-text: 3818 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.4</div>
                                                        <div class="over-circle high-score"><span class="over-score">4</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Fakhar Zaman, <b>FOUR</b> runs, sliced away to the offside. Looked too close to cut, and there was no timing, but surprising misfield in the covers lets the ball squirt through to the boundary. Morgan the man who misfielded</div>
                                                </div><!-- react-text: 3826 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.3</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Fakhar Zaman, no run, less room this time. Short and straighter, but Fakhar shoulders arms</div>
                                                </div><!-- react-text: 3834 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item  ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.2</div>
                                                        <div class="over-circle low-score"><span class="over-score">1</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Imam-ul-Haq, 1 run, similar line and length, cut away to third man, and Imam is up and running</div>
                                                </div><!-- react-text: 3842 -->
                                                <!-- /react-text -->
                                            </div>
                                            <div class="commentary-item pre ">
                                                <div class="item-wrapper">
                                                    <div class="over">
                                                        <div class="time-stamp">0.1</div>
                                                        <div class="over-circle low-score"><span class="over-score">0</span></div>
                                                    </div>
                                                    <div class="description">Woakes to Imam-ul-Haq, no run, wide outside off stump, Imam lets it go safely by</div>
                                                </div>
                                                <div>
Quote
#2
There's no question in your post, nor have you shown us what you've tried, but you can at least use Beautiful Soup to parse the HTML and extract the data you want from it.
Quote
#3
Can also take a look at Web-Scraping part-1.
Quote
#4
(Aug-18-2019, 01:35 PM)ndc85430 Wrote: There's no question in your post, nor have you shown us what you've tried, but you can at least use Beautiful Soup to parse the HTML and extract the data you want from it.

below is the code I've tried until now (sorry I forgot to attach in the previous post), however the result includes additional strings as well which I didn't exactly want, I just need the player name from this string => ' to [Player Name], '

import numpy as mp
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import re

fname = 'PakVEng.html'
HtmlFile = open(fname, 'r', encoding='utf-8')
source_code = HtmlFile.read()
HtmlFile.close()

testVariable = str(source_code)
vanillaData = testVariable.split('window.__INITIAL_STATE__ = {', 1)[0] + 'window.__INITIAL_STATE__ = {'

totalCount = re.search(' to (.*), ', vanillaData)
print(totalCount)
This code gives me the following result

ahmedwaqas92@ideapad:~/Downloads$ python3 ImportHtml.py 
<_sre.SRE_Match object; span=(268692, 268742), match=' to Shadab Khan, <b>FOUR</b> runs, not quite 350,>
ahmedwaqas92@ideapad:~/Downloads$
The html file is on my computer for testing purposes only, if you want to take a look then go here is the link. All I need from this HTML file is all the player names which occur between ' to [player name], ' and the runs scored after it. This process repeats itself multiple times in the html file so I would want to count all of such instances and then put them in the form of a table.

(Aug-18-2019, 04:49 PM)snippsat Wrote: Can also take a look at Web-Scraping part-1.

I am successfully able to scrape the data onto a string variable, its just that I am having trouble in determining on how to extract sub strings that I want from the html file that I have obtained using Firefox driver selenium scroll and using page source.

This question was part of a larger project that I am working on independently. Below is the link of another question I asked on this thread.

https://python-forum.io/Thread-How-to-Ca...1#pid89131
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  using paramater markers masmithrpg 0 91 Dec-31-2019, 01:57 PM
Last Post: masmithrpg
  Copy a File & Increment File Count xingfang 1 227 Oct-29-2019, 03:58 PM
Last Post: DeaD_EyE
  Adding markers to Folium map only adding last element. tantony 0 157 Oct-16-2019, 03:28 PM
Last Post: tantony
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 261 Oct-14-2019, 10:40 AM
Last Post: perfringo
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 251 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 290 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  read text file and write into html with correct link jacklee26 4 401 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Find a given file recursively inside a directory alinaveed786 1 445 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Find string and add character - newbi PyDK 1 285 May-15-2019, 01:22 PM
Last Post: ichabod801
  How can I find a string in sequence (with Booleans)? go127a 3 384 Apr-23-2019, 01:58 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)