2

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
11
  • You can use pandas library it has method called df.to_csv("filename.csv") using which you can save it to csv Commented Aug 15, 2018 at 15:06
  • 3
    Don't print to CSV. Use the csv package instead of reinventing the wheel. Commented Aug 15, 2018 at 15:09
  • @zvone - would you mind telling me where should I change my code so I can save time not reinventing the wheel ? Commented Aug 15, 2018 at 15:18
  • anyone that could guide me to the right path, please ? Commented Aug 16, 2018 at 7:00
  • @Andie31 please solve indent problem in last 4 lines of your code Commented Aug 16, 2018 at 10:31

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

21 Comments

if you want further explaination of csv writer you can refer my previous answer with this link
trying to piece all together with the first part of code, having issues :( AttributeError: module 'csv' has no attribute 'DictWriter' on line 15 Do u mind checking it ?
use import csv
i did :( no luck
you forgot to include iframe = driver.find_elements_by_tag_name('iframe')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.