我有两份印刷品要写入一个 CSV 文件中,分别写入 A 列和 B 列中
我的问题是,当我同时打印(第一张和第二张)时,我只能得到一个元素,我猜多次,因为它不在循环内。
print((text), (link[0:-9]))
结果:
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
我的第一次打印是这样的:我想将其打印到列 A
PWFERT-BK
PMFEP-BK
LMF8CC-BL
PMFEP-GY
ASPCP-NV
LWBASK-PK
LWBATA-PK
LWBATOP-PK
LMF8CC-RD
我的第二张照片看起来像这样:我想将其打印到列 B
LMFCIIC
LWBASK
LWBATA
LWBATOP
LMFCIIC
这是我的完整代码:
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]))
答案 0 :(得分:1)
编写driver.switch_to.frame(i)
时,基本上就是在访问iframe html元素。像普通的html页面一样,您也可以访问其内部元素。
您上一个问题中的iframe就像
<body>
<a href="http://www.test2.com" target="_blank">
<img src="https://img2.test2.com/LWBAD-1.jpg"></a>
<br/>Code: LWBAD
您可以通过以下方式轻松访问图片网址
img_src = driver.find_element_by_tag_name('img').get_attribute('src')
并将其存储在csv文件中
代码:
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()