I want to replace a word from the text taken by css selector like
company_name = browser.find_element_by_css_selector("body > company.b1").text
let's it took the text like this "GBH Global"
description = browser.find_element_by_css_selector("body > p.b1").text
let's it took the text like this "This is companyname we are based in london"
and I want to do this and replace the company name with GBH Global like "This is GBH Global we are based in london"
company_description = browser.find_element_by_css_selector("body > p.b1 > input")
company_description.send_keys(description)
I want to send like this ""This is GBH Global we are based in london" using selenium and python
I have this text "This is companyname we are based in london" and I can change its format for the code to work it properly...
Presumably the text extracted by the line of code:
browser.find_element_by_css_selector("body > company.b1").text
i.e. GBH Global would be a variable. In that case you can replace the text companyname as follows:
company_name = browser.find_element_by_css_selector("body > company.b1").text
description = browser.find_element_by_css_selector("body > p.b1").text
text_to_replace = browser.find_element_by_css_selector("body > company.b1").text.split()[2]
print(description.replace(text_to_replace, "{}".format(company_name)))
Following should work
company_name = "GBH Global"
description = "This is companyname we are based in london"
company_description = description.replace("companyname", company_name)
Related
I am e-mailing a dataframe in the body of the e-mail. However, I want to format the dataframe results so that if my results are a specific value (non-0 value) then the text turns red. I got help earlier that does convert all of my dataframe results to red but now i need to make the text red for SPECIFIC functions if they output a non-value.
Please see below for my function which I turn into a dataframe to be sent in an e-mail
Please see below for my function which I turn into a dataframe to be sent in an e-mail:
def record_count():
with open('filename.csv', encoding='utf8') as file:
record_number = len(list(file))
print("Number of records on file:", record_number)
return record_number
def id_check():
df = pd.read_csv('filename.csv', low_memory=False)
missing_id = df["id"].isna().sum()
print("Number of users missing an id:", missing_id)
return missing_id
def name_check():
df = pd.read_csv('filename.csv', low_memory=False)
missing_name = derivs["name"].isna().sum()
print("Number of users missing a name:", missing_name)
return missing_name
checks_table = {
'Check' : ['Record Check', 'ID Check', 'Name Check'],
'Summary' : ['Number of records on file','Number of records missing an ID', 'Number of users missing a name'],
'Findings' : [record_count(), id_check(), name_check()]
}
df_checks = pd.DataFrame.from_dict(checks_table)
df_checks.head()
result looks like:
Check Summary Findings
0 Record Count Number of records on file 10000
1 ID Check Number of records missing an ID 10
2 Name Check Number of records missing a name 20
My current html e-mail code works so that if ANY of my findings != 0, the entire line is red but is there a way to make it so that it only applies to specific checks? I only want the entire line for ID check and Name check to turn red if the findings corresponding to those functions != 0.
See below for my html e-mail code:
msg = MIMEMultipart('mixed')
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = ','.join(TO)
color= lambda x: (pd.DataFrame('', index=x.index, columns=x.columns)
.mask(x['Findings'].ne(0), 'color:red;'))
html = df_checks.style.apply(color, axis=None).hide_index().render()
#this formats the e-mail header and adds the QC summary from the previous cell into the body of the e-mail
html = f"""
<html><body>
<h1> </h1>
<p> Please see the checks summary below {html} </p>
</body></html>
"""
I want to extract menu_header from bubble_list and then need to extract all titles from menu_list with condition type='menu' only
response3 = [{"thumbs_id":56071,"disable_text":"yes","thumbs_display":"no","recipient_id":"12698","bubble_list":[{"class":"bubble-top","delay":0,"logo":"no","text":"You may only add\/change your preferred first name and legal last name in PIMS.","type":"text"},{"class":"bubble-menu","delay":3000,"logo":"yes","menu_header":"Which name would you like to update?","menu_list":[{"payload":"\/update_name{\"name_type\":\"preferred_first_name\"}","title":"Update Preferred First Name"},{"payload":"\/update_name{\"name_type\": \"legal_last_name\"}","title":"Update Legal Last Name"}],"menu_status":"always_active","type":"menu"}],"button_list":[{"payload":"\/inactivity_timeout","title":"End Chat"},{"payload":"\/bot_help{\"bot_help_value\":\"start_over\"}","title":"Start Fresh again"}],"related_question":[]}]
A bit long but it works:
response = {key:value for key, value in response3[0].items() if key == 'bubble_list'}
filter = [my_dict for my_dict in response['bubble_list'] if 'menu_header' in list(my_dict.keys()) and my_dict.get('type') == 'menu']
menu_header = filter[0]['menu_header']
menu_list = [title.get('title') for title in filter[0]['menu_list']]
I need split a substring from a string, exactly this source text:
Article published on: Tutorial
I want delete "Article published on:" And leave only
Tutorial
, so i can save this
i try with:
category = items[1]
category.split('Article published on:','')
and with
for p in articles:
bodytext = p.xpath('.//text()').extract()
joined_text = ''
# loop in categories
for each_text in text:
stripped_text = each_text.strip()
if stripped_text:
# all the categories together
joined_text += ' ' + stripped_text
joined_text = joined_text.split('Article published on:','')
items.append(joined_text)
if not is_phrase:
title = items[0]
category = items[1]
print('title = ', title)
print('category = ', category)
and this don't works, what im missing?
error with this code:
TypeError: 'str' object cannot be interpreted as an integer
You probably just forgot to assign the result:
category = category.replace('Article published on:', '')
Also it seems that you meant to use replace instead of split. The latter also works though:
category = category.split(':')[1]
I've created a basic counter for words in a song, but am having trouble formatting the album title and artist name from a given page on this lyrics website. Here's an example of what I am focused on:
I want to format it in this way:
Album Title: [Album Title] (Release_year)
Artist: [Artist Name]
I'm running into two problems:
The album title isn't enclosed in its own tag, so if I call the h1 tag I get both the album name, release year and artist name. How do I call them separately, or how do I break them up when calling them?
The album name has two blank lines and two blank spaces included in the string. How do I get rid of them? The release year prints right next to the album title, which is exactly what I'm looking for, but I cant get the album title to format properly.
This is what I currently have:
song_artist = soup.find("a",{"class":"artist"}).get_text()
album_title = soup.find("h1",{"class":"album_name"}).get_text()
print "Album Title: " + str(album_title)
print "Song Artist: " + str(song_artist.title())
which produces:
Thank you!!
album_title = soup.find("h1",{"class":"album_name"}).find(text=True).strip()
album_year = soup.find("span",{"class":"release_year"}).get_text().strip()
print 'Album Title: {} {}'.format(album_title, album_year)
I am using Pyparsing module and the nestedExpr function in it.
I want to give a delimitter instead of the default whitespace-delimited in the content argument of nestedexpr function.
If I have a text such as the following
text = "{{Infobox | birth_date = {{birth date and age|mf=yes|1981|1|31}}| birth_place = ((Memphis, Tennessee|Memphis)), ((Tennessee)), U.S.| instrument = ((Beatboxing)), guitar, keyboards, vocalsprint expr.parse| genre = ((Pop music|Pop)), ((contemporary R&B|R&B))| occupation = Actor, businessman, record producer, singer| years_active = 1992–present| label = ((Jive Records|Jive)), ((RCA Records|RCA)), ((Zomba Group of Companies|Zomba))| website = {{URL|xyz.com|Official website}} }}"
When I give nestedExpr('{{','}}').parseString(text) I need the output as the following list:
['Infobox | birth_date =' ,['birth date and age|mf=yes|1981|1|31'],'| birth_place = ((Memphis, Tennessee|Memphis)), ((Tennessee)), U.S.| instrument = ((Beatboxing)), guitar, keyboards, vocalsprint expr.parse| genre = ((Pop music|Pop)), ((contemporary R&B|R&B))| occupation = Actor, businessman, record producer, singer| years_active = 1992–present| label = ((Jive Records|Jive)), ((RCA Records|RCA)), ((Zomba Group of Companies|Zomba))| website =',[ 'URL|xyz.com|Official website' ]]
How can I give a ',' or '|' as the delimmiter instead of the whitespace-delimited characters? I tried giving the characters but it didnt work.