[英]selenium python return all outputs
The purpose of this code is to scrape a bunch of URLs then extract the title from every web page. 此代码的目的是抓取一堆URL,然后从每个网页中提取标题。 Then use the outputs in another function. 然后在另一个函数中使用输出。
Here is the code: 这是代码:
from selenium import webdriver
class DataEngine:
def __init__(self):
self.urls = open(r"C:\Users\Sayed\Desktop\script\links.txt").readlines()
self.driver = webdriver.Chrome(r"D:\Projects\Tutorial\Driver\chromedriver.exe")
def title(self):
for url in self.urls:
self.driver.get(url)
title = self.driver.find_element_by_xpath('//*[@id="leftColumn"]/h1').text
return title
def rename(self):
names = self.title()
for name in names:
print(name)
x = DataEngine()
x.rename()
Here is what I expected: 这是我所期望的:
Title (1) 标题(1)
Title (2) 标题(2)
Title (3) 标题(3)
Title (4) 标题(4)
Here is the output: 这是输出:
T Ť
i 一世
t Ť
l 升
e Ë
( (
1 1个
) )
Build a list of the results for each URL, currently you only returning one (the first) result which is why it is printing like that: 为每个URL建立一个结果列表,当前您仅返回一个(第一个)结果,这就是为什么要这样打印的原因:
from selenium import webdriver
class DataEngine:
def __init__(self):
self.urls = open(r"C:\Users\Sayed\Desktop\script\links.txt").readlines()
self.driver = webdriver.Chrome(r"D:\Projects\Tutorial\Driver\chromedriver.exe")
def title(self):
titles = []
for url in self.urls:
self.driver.get(url)
title = self.driver.find_element_by_xpath('//*[@id="leftColumn"]/h1').text
titles.append(title)
return titles
def rename(self):
names = self.title()
for name in names:
print(name)
x = DataEngine()
x.rename()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.