<p>在这里,我首先用<code>find_elements_by_xpath</code>获得每个页面的名称、国家和网站,并将它们存储到一个列表中。将从列表中的每个元素中提取文本,并将值添加到新列表中。你知道吗</p>
<pre><code>from selenium import webdriver
MAX_PAGE_NUM = 10
driver = webdriver.Chrome('C:\\Users...\\chromedriver.exe')
names_list = list()
Countries_list = list()
websites_list = list()
# The for loop is to get each of the 10 pages
for i in range(1, MAX_PAGE_NUM):
page_num = str(i)
url = "https://www.oilandgasnewsworldwide.com/Directory1/DREQ/Drilling_Equipment_Suppliers_?page=" + page_num
driver.get(url)
# Use "driver.find_elements" instead of "driver.find_element" to get all of them. You get a list of WebElements of each page
names = driver.find_elements_by_xpath("//*[@class='fontsubsection nomarginpadding lmargin opensans']")
# To get the value of each WebElement in the list. You have to iterate on the list
for i in range(0, len(names)):
# Now you add each value into a new list
names_list.append(names[i].text)
Countries = driver.find_elements_by_xpath("//td[text()='Country:']/following-sibling::td")
for i in range(0, len(Countries)):
Countries_list.append(Countries[i].text)
websites = driver.find_elements_by_xpath("//td[text()='Website:']/following-sibling::td")
for i in range(0, len(websites)):
websites_list.append(websites[i].text)
print(names_list)
print(Countries_list)
print(websites_list)
driver.close()
</code></pre>
<p>我希望这对你有用</p>
<p><strong>选项:</strong>获取<code><div class = border fontcontentdet></code>上包含的节中的所有数据。你知道吗</p>
<p>从selenium导入webdriver</p>
<pre><code>MAX_PAGE_NUM = 10
driver = webdriver.Chrome('C:\\Users\\LVARGAS\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\chromedriver.exe')
data_list = list()
# The for loop is to get each of the 10 pages
for i in range(1, MAX_PAGE_NUM):
page_num = str(i)
url = "https://www.oilandgasnewsworldwide.com/Directory1/DREQ/Drilling_Equipment_Suppliers_?page=" + page_num
driver.get(url)
rows = driver.find_elements_by_xpath("//*[@class='border fontcontentdet']")
for i in range(0, len(rows)):
print(rows[i].text)
data_list.append(rows[i].text)
print(' -')
driver.close()
print(data_list)
</code></pre>