简体   繁体   中英

Python Selenium store data to specific column in CSV?

I have two prints that I want to write to a single CSV File into Column A and Column B

My problem is when I print both(first and second print) at the end , I get only an element, multiple times I guess because it's not inside a loop or so.

print((text), (link[0:-9]))

Result :

LMFCIIC PWFERT-BK
LMFCIIC PMFEP-BK
LMFCIIC LMF8CC-BL
LMFCIIC PMFEP-GY
LMFCIIC ASPCP-NV
LMFCIIC LWBASK-PK
LMFCIIC LWBATA-PK
LMFCIIC LWBATOP-PK
LMFCIIC LMF8CC-RD

My first print looks like this : And I want to print it to Column A

PWFERT-BK
PMFEP-BK
LMF8CC-BL
PMFEP-GY
ASPCP-NV
LWBASK-PK
LWBATA-PK
LWBATOP-PK
LMF8CC-RD

My Second print looks like this : And I want to print it to Column B

LMFCIIC


LWBASK
LWBATA
LWBATOP
LMFCIIC

Here is my full code :

from bs4 import BeautifulSoup
from selenium import webdriver
import html5lib
import time
import requests

driver_path = '/usr/local/bin/chromedriver 2'
driver = webdriver.Chrome(driver_path)
driver.implicitly_wait(10)

driver.get('https://www.tenniswarehouse-europe.com/zzz/producttracker_bl.html?ccode=SWIMG030')
try:
    iframe = driver.find_elements_by_tag_name('iframe')
    for i in range(0, len(iframe)):
            f = driver.find_elements_by_tag_name('iframe')[i]
            driver.switch_to.frame(i)
            #  your work to extract link
            text = driver.find_element_by_tag_name('body').text
            text = text.replace("Code: ","")
            text = text.replace("No Copy Images to TW Server","")
            print(text)
            driver.switch_to_default_content()
finally:
    driver.quit()

resp = requests.get('https://www.tenniswarehouse-europe.com/zzz/producttracker_bl.html?ccode=SWIMG030')
soup = BeautifulSoup(resp.text,"lxml")
for frame in soup.findAll('img'):
    link = (frame['src'])
    link = link.split('=')[1] 
    print ((link[0:-9]))
  • I used www.example.com because the link is not accessible out of my network

when you write driver.switch_to.frame(i) you are basically accessing iframe html element. like normal html page you can access its inside element as well.

from your previous question iframe was like

<body>
<a href="http://www.test2.com" target="_blank">
<img src="https://img2.test2.com/LWBAD-1.jpg"></a>
<br/>Code: LWBAD

you can easily access image url by

img_src = driver.find_element_by_tag_name('img').get_attribute('src')

and store that in csv file

code:

from bs4 import BeautifulSoup
from selenium import webdriver
import html5lib
import time
import requests
import csv

driver_path = '/usr/local/bin/chromedriver 2'
driver = webdriver.Chrome(driver_path)
driver.implicitly_wait(10)

driver.get('https://www.example.com')

iframe = driver.find_elements_by_tag_name('iframe')
images = driver.find_elements_by_tag_name('img')
with open('file_name.csv', 'w', newline='') as csvfile:
    field_names = ['text', 'src']
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writerow({'text': 'text', 'src': 'src'})
    for i in range(0, len(iframe)):
        f = driver.find_elements_by_tag_name('iframe')[i]
        img_src = images[i].get_attribute('src')

        # do the src splitting here
        img_src = img_src.split('=')[1]

        driver.switch_to.frame(i)

        text = driver.find_element_by_tag_name('body').text


        text = text.replace("Code: ", "")
        text = text.replace("No Copy Images to TW Server", "")
        print(text)
        writer.writerow({'text': text, 'src': img_src})

        driver.switch_to_default_content()
driver.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM