Python Forum
Good way to ignore case when searching elements?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Good way to ignore case when searching elements?
#1
Hello,

In the files I need to work with, I notice that the values in the following meta line can be either lower-case or capitalized.

What would be the right way to convert them to lower-case regardless so that search doesn't miss the others?

#could be "Content-Type" or "content-type"
meta = soup.head.find("meta",  {"http-equiv":"content-type"})
if meta is None:
  print("here1")
else:
  print("here2")
Thank you.
Reply
#2
You might could try something like this

from bs4 import BeautifulSoup

html_doc = """ 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type"  charset="UTF-8">
    <meta http-equiv="Content-Type"  charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
 """

soup = BeautifulSoup(html_doc, 'lxml')
tags = ['content-type', 'Content-Type']
for tag in tags:
    meta = soup.head.find('meta', {'http-equiv': tag})
    print(meta)
output
Output:
<meta charset="utf-8" http-equiv="content-type"/> <meta charset="utf-8" http-equiv="Content-Type"/>
A way without looping
from bs4 import BeautifulSoup

html_doc = """ 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type"  charset="UTF-8">
    <meta http-equiv="Content-Type"  charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
 """

soup = BeautifulSoup(html_doc, 'lxml')

meta = soup.select('meta[http-equiv="content-type" i]')

print(meta)
output
Output:
[<meta charset="utf-8" http-equiv="content-type"/>, <meta charset="utf-8" http-equiv="Content-Type"/>]
Pedroski55 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 8,345 May-17-2022, 11:38 AM
Last Post: Larz60+
  Ignore WakeWord after it's said Extra 2 2,068 Apr-01-2022, 12:32 AM
Last Post: Extra
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 4,385 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  Switch case or match case? Frankduc 9 7,713 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Sorting Elements via parameters pointing to those elements. rpalmer 3 3,500 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Ignore first few letters of a line when reading file. ShakeyPakey 16 11,595 May-30-2020, 02:17 PM
Last Post: BitPythoner
  How to ignore empty columns from DB? Winfried 1 3,036 May-15-2020, 08:35 PM
Last Post: menator01
  How to make the script ignore down devices. wagnergt12 4 5,016 Apr-20-2020, 11:45 PM
Last Post: wagnergt12
  Regex ignore JohnnyCoffee 1 3,588 Mar-16-2020, 12:16 PM
Last Post: scidam
  Ignore Folder Evil_Patrick 3 5,181 Oct-29-2019, 07:44 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020