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
Nihal
  • 5,262
  • 7
  • 23
  • 41
Andie31
  • 305
  • 3
  • 13
  • You can use pandas library it has method called df.to_csv("filename.csv") using which you can save it to csv – ANISH TIWARI Aug 15 '18 at 15:06
  • 3
    Don't `print` to CSV. Use the [`csv` package](https://docs.python.org/3/library/csv.html) instead of reinventing the wheel. – zvone Aug 15 '18 at 15:09
  • @zvone - would you mind telling me where should I change my code so I can save time not reinventing the wheel ? – Andie31 Aug 15 '18 at 15:18
  • anyone that could guide me to the right path, please ? – Andie31 Aug 16 '18 at 07:00
  • @Andie31 please solve indent problem in last 4 lines of your code – Nihal Aug 16 '18 at 10:31
  • and how are you getting your second print? – Nihal Aug 16 '18 at 10:33
  • @Nihal I updated the code. There's to #print commands in the code. Line 21, Line 31 - I just don't know how to put everything in a loop :( – Andie31 Aug 16 '18 at 10:40
  • `resp = requests.get('https://www.example.com') soup = BeautifulSoup(resp.text,"lxml") for frame in soup.findAll('img'): link = (frame['src']) link = link.split('=')[1] print ((link[0:-9]))` tell me the exact purpose of this part of code – Nihal Aug 16 '18 at 10:41
  • I need a part of some URLs that are not inside the frame, they are just in the simple html source. So I decided not to use selenium driver for that reason :( does that make sense ? – Andie31 Aug 16 '18 at 10:44
  • you can access it BTW using selenium. – Nihal Aug 16 '18 at 10:45
  • so I can do it both Iframe and regular source in one loop ? Can you help me with that please ? – Andie31 Aug 16 '18 at 10:46

1 Answers1

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()
Nihal
  • 5,262
  • 7
  • 23
  • 41