-1

In the below code I'm extracting emails id and storing in the list in python

chrome_driver_path = os.path.abspath('..')  + "\\Drivers\\chromedriver.exe"

driver=webdriver.Chrome(chrome_driver_path)
driver.maximize_window()
driver.get("http://www.airindia.in/contact-details.htm")
driver.implicitly_wait(3)

doc = driver.page_source

emails = re.findall(r'[\w\.-]+@[\w\.-]+', doc)
list_new = []
for email in emails:
    list_new.extend(str(email))
    #print(email)

print("total emails - ",len(list_new))

driver.quit()

print(list_new)

but getting the output as ['c', 'a', 'l', 'l', '.', 'd', 'e', 'l', '@', 'a', 'i', 'r', 'i', 'n']

I need output as ['call.del@airindia.in','airindiaretros.ai@iclployalty.com']

I'm new to selenium python

sample code with my console output https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/RegularExpression.py

venkatesh
  • 93
  • 3
  • 11
  • What is the output of print("total emails - ",len(list_new)) Does it show one email address or many? – Watty62 May 23 '18 at 07:43
  • https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/RegularExpression.py – venkatesh May 23 '18 at 07:48

5 Answers5

2

Use list.append() to append elements to a list.

Use list.extend() to append a bunch of elements (from a list of elements) to a list

Moberg
  • 5,253
  • 4
  • 38
  • 54
2

Change

list_new.extend(str(email))

To:

list_new.append(str(email))

append adds its argument as a single element to the end of a list. The length of the list itself will increase by one.

extend iterates over its argument adding each element to the list, extending the list. 

This post might help you.

Austin
  • 25,759
  • 4
  • 25
  • 48
1

You should not use regex for this purpose. Simply try below to get list of emails:

emails = [email.text for email in driver.find_elements_by_class_name('linkText') if "@" in email.text]
Andersson
  • 51,635
  • 17
  • 77
  • 129
1

Extends() should become a list of elements as an input. That is why python threats your String as a list of characters and appends every single one to the list.

You should use append(). I takes one element and appends it on the end of a list.

Also I can recommend you to update your re pattern. I am using this one:

re_pattern = r'[\w\.-]+@[\w\.-]+\.[\w\.]+'

But take a look at "Email Address Regular Expression That 99.99% Works"

Superluminal
  • 947
  • 10
  • 23
0

You use list.extend() which treats the string as a list of chars. Use list.append() instead to append the string as a single object.

Syntaxén
  • 465
  • 1
  • 8
  • 15