Home>

Hello. I'm a python beginner.
I want to store the scraped data in selenium in order and output it, but it doesn't work.

Currently in the arry list with the corresponding code below
arry =
["Text element", "Text element", "Text element" ....], ["Number", "Number", "Number", ...], ["Text element 2" "Text element 2" "Text element 2" ....]
It is output like this, but for me
arry =
[["Text element", "Number", "Text element 2"] ["Text element", "Number", "Text element 2"] ["Text element", "Number", "Text element 2"]. .....]
I want to store each set in the list like this, what should I do? ?? Would you please help me? ?? Thank you.

Corresponding source code
from selenium import webdriver
import chromedriver_binary
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import urllib.request
options = Options ()
options.headless = True
options.add_argument ('--headless')
driver = webdriver.Chrome (options = options)
driver.get ("Scraping site URL")
arry = []
selector ='text element'
element = driver.find_elements_by_css_selector (selector)
element = [a.get_attribute ("href") for a in element]

date_element = "number"
dates = driver.find_elements_by_css_selector (date_element)
dates = [b.text for b in dates]
i ='text element 2'
items = driver.find_elements_by_css_selector (i)
items = [c.text for c in items]
arry.append ((element, dates, items))
print (arry)
for q in arry:
print (q)
What I tried

Should I turn it with a for statement? And finally
for q in arry:
print (q)
I wrote code like this and the result was the same. I'm sorry, I would like to ask you to teach. Thank you.

Supplementary information (FW/tool version, etc.)

mac10.15.3
python3.8

  • Answer # 1

    Ref: https://docs.python.org/ja/3/library/functions.html#zip

    zipped = zip (element, dates, items)
    print (zipped) # [("text element", "number", "text element 2"), ("text element", "number", "text element 2")]

    I should do it.
    But,elementWhendatesWhenitemsI don't think they are the same number,
    The original format doesn't seem to be a problem.

  • Answer # 2

    Whether to use zip () as unhappychoice answers
    Or using numpy.TIt is possible to transpose as follows.

    import numpy as np
    array = [['a','b','c'], [1, 2, 3], ['a','i','u']]
    print (np.array (array) .T)
    >>>[['a' '1''a']
    >>>['b' '2''I']
    >>>['c' '3''u']]