Get the number of searches with a regular expression. SeleniumBasic VBA Python

Summary of regular expressions often used when scraping with Selenium Basic

Scheduled to be added at any time

■ Get the number of searches

Example: Enter "Shinjuku" on the tabelog search screen to get the number of items that appear. Search by tabelog Search results: 1 to 20 are displayed / 4664 in total I want to get this "4664".

VBA operation with Windows10 Excel

Preparation: Remove double-byte characters and line feed codes from the character string to be searched to simplify regular expressions (VBA)

vba


 Dim mozi as String
 mozi = "Search results: 1 to 20 are displayed / 4664 in total"     'Target sentence
  
 mozi = WorksheetFunction.Clean(mozi)  'Delete line feed code, etc.
 mozi = Replace(mozi, " ", "")     'Delete half-width space
 mozi = Replace(mozi, " ", "")         'Delete double-byte space

 Debug.Print (mozi)  'Search results: 1 to 20 items are displayed / 4664 items in total
'↑ Useless white space has been removed.

(2) Search results: Display 1 to 20 items / Get 4664 items in total 4664 items using regular expressions (VBA)

vba



Dim re As RegExp
Set re = New RegExp
Dim pattern As String: pattern = "all(\d+)Case"     'Regular expression pattern
Dim mc As MatchCollection
Dim m As Match
Dim Matches As MatchCollection

'Regular expression specification
With re      
  .pattern = pattern
  .IgnoreCase = False     'Is it case sensitive?(False), Do not(True)
  .Global = True          'Do you want to search the entire string(True), Do not(False)
End With    
 
 Set Matches = re.Execute(mozi) 'Execute regular expression matching to the character string prepared in ↑
   
 If Matches.Count > 0 Then
    
  Debug.Print (Matches.Item(0))  'All 4669 cases
'↑ At this point, there are "4669 cases in total". So
'From this, the seating expression pattern()I want to take out only the numbers that are enclosed
  set m = Matches.Item(0)
  Debug.Print (m.SubMatches(0))’4669 I got it
 End If

For Python

environment Windows Python3.8.3

python


import re

mozi="Display 1 to 20 cases / 4664 cases in total"
pattern = "all(\d+)Case"
mozi=mozi.replace(" ","")  #Delete half-width space
mozi=mozi.replace(" ","") #Delete double-byte space
ptn=re.compile(pattern)   #Prepare regular expression pattern Pattern object is returned

if result := ptn.search(mozi):    #Search execution: None if there is no Match object if it matches
    print(result.group(0))        #All 4664 cases Matched character strings
    print(result.group(1))        #4664   ()Enclosed part

Extraction of all XX cases is often used. Other regular expressions that are often used will be added as needed. I would like to point out if there is an easier way.

Recommended Posts

Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get the number of specific elements in a python list
Get the number of readers of a treatise on Mendeley in Python
Get the matched string with a regular expression and reuse it when replacing on Python3
Get a capture of the entire web page in Selenium Python VBA
Get the caller of a function in Python
Calculate the total number of combinations with python
[Python] Get rid of dating with regular expressions
Get the number of articles accessed and likes with Qiita API + Python
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
Get the stock price of a Japanese company with Python and make a graph
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Make one repeating string with a Python regular expression.
How to get the number of digits in Python
Destroy the intermediate expression of the sweep method with Python
Get the size (number of elements) of UnionFind in Python
Get the operation status of JR West with Python
A python regular expression, or a memo of a match object
[Python] Get the number of views of all posted articles
Calculate the product of matrices with a character expression?
Get the number of digits
Regular expression manipulation with Python
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
Determine if a string is a time with a python regular expression
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
Get a list of purchased DMM eBooks with Python + Selenium
Python --Find out number of groups in the regex expression
Get rid of dirty data with Python and regular expressions
[Homology] Count the number of holes in data with Python
Get the source of the page to load infinitely with python.
Get the number of views of Qiita
Get the weather with Python requests
Get the weather with Python requests 2
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
String replacement with Python regular expression
Get the number of Youtube subscribers
Get the number of PVs of Qiita articles you posted with API
Save the result of the life game as a gif with python
[python, ruby] fetch the contents of a web page with selenium-webdriver
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
The story of making a standard driver for db with python.
[Python] Get the update date of a news article from HTML
Get the URL of a JIRA ticket created with the jira-python library
The idea of feeding the config file with a python file instead of yaml
Get a list of files in a folder with python without a path
The story of making a module that skips mail with python
Create a compatibility judgment program with the random module of python.
PhytoMine-I tried to get the genetic information of plants with Python
[Python] A program that finds the maximum number of toys that can be purchased with your money
Check the existence of the file with python
[Python] Get the variable name with str
Search the maze with the python A * algorithm
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
[Python] Get the character code of the file