Python 3.x 使用selineum webdriver python打开链接并在现有网页中刮取数据
我正在学习使用Python的Selenium web驱动程序进行web抓取。为了我的学习目的,我正在搜刮www.com。我正在抓取职位、公司名称、地点、薪水和工作总结。我可以提取职位,公司名称,地点,工资使用美丽的汤。作业摘要正在加载到下一页,我尝试使用selenium提取数据,但没有成功。我已经检查了这里的所有帖子,但仍然无法完成。我可以单击新页面,但我不确定如何从新页面中刮取数据 我的代码Python 3.x 使用selineum webdriver python打开链接并在现有网页中刮取数据,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,我正在学习使用Python的Selenium web驱动程序进行web抓取。为了我的学习目的,我正在搜刮www.com。我正在抓取职位、公司名称、地点、薪水和工作总结。我可以提取职位,公司名称,地点,工资使用美丽的汤。作业摘要正在加载到下一页,我尝试使用selenium提取数据,但没有成功。我已经检查了这里的所有帖子,但仍然无法完成。我可以单击新页面,但我不确定如何从新页面中刮取数据 我的代码 #Importing necessary library from selenium import
#Importing necessary library
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
import pandas as pd
import time
import re
import requests
from itertools import zip_longest
from webdriver_manager.chrome import ChromeDriverManager
title = []
company = []
locations = []
summary = []
for pageno in range(0,26):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://nz.indeed.com/jobs?q=data+analyst&l=New+Zealand&start=" + str(10*pageno))
time.sleep(1)
summaryItems = driver.find_elements_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_links = [summaryItem.get_attribute("href") for summaryItem in summaryItems]
for job_link in job_links:
driver.get(job_link)
time.sleep(1)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']").text
title.append(job_title)
company_name = driver.find_element_by_xpath("//div[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']").text
company.append(company_name)
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']").text
locations.append(location)
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']").text
summary.append(job_description)
driver.close()
# Converting all the details into dataframe and csv file
final = []
for item in zip_longest(title, company, locations, summary):
final.append(item)
df4 = pd.DataFrame(
final, columns=['Job_title', 'Company_name','Locations', 'Summary'])
#df.to_csv('booked.csv')
I tried to debug but not successful. One of the job page is not loading. I don't know the reason. Problem either 3 or 4 loop. Any suggestion?
我可以单击新页面,但我不确定如何从新页面中刮取数据。我需要做的其他网页以及自动。有什么建议吗?您可以通过Selenium获得该作业的链接并重新加载,而不是单击在新页面上打开作业,如下所示:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://nz.indeed.com/jobs?q=data+scientist&l=New+Zealand&start=" + str(10*1))
time.sleep(5)
summaryItems = driver.find_element_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_link = summaryItems.get_attribute("href")
driver.get(job_link)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']")
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']")
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']")
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']")
summaryItems = driver.find_elements_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_links = [summaryItem.get_attribute("href") for summaryItem in summaryItems]
for job_link in job_links:
driver.get(job_link)
time.sleep(1)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']").text
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']").text
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']").text
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']").text
现在,您可以轻松获取工作信息,如下所示:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://nz.indeed.com/jobs?q=data+scientist&l=New+Zealand&start=" + str(10*1))
time.sleep(5)
summaryItems = driver.find_element_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_link = summaryItems.get_attribute("href")
driver.get(job_link)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']")
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']")
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']")
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']")
summaryItems = driver.find_elements_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_links = [summaryItem.get_attribute("href") for summaryItem in summaryItems]
for job_link in job_links:
driver.get(job_link)
time.sleep(1)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']").text
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']").text
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']").text
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']").text
编辑:
如果要获取所有工作列表的信息,可以按如下方式进行:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://nz.indeed.com/jobs?q=data+scientist&l=New+Zealand&start=" + str(10*1))
time.sleep(5)
summaryItems = driver.find_element_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_link = summaryItems.get_attribute("href")
driver.get(job_link)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']")
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']")
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']")
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']")
summaryItems = driver.find_elements_by_xpath("//a[contains(@class, 'jobtitle turnstileLink')]")
job_links = [summaryItem.get_attribute("href") for summaryItem in summaryItems]
for job_link in job_links:
driver.get(job_link)
time.sleep(1)
job_title = driver.find_element_by_xpath("//*[@class='icl-u-xs-mb--xs icl-u-xs-mt--none jobsearch-JobInfoHeader-title']").text
company_name = driver.find_element_by_xpath("//*[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']").text
location = driver.find_element_by_xpath("//*[@class='jobsearch-JobMetadataHeader-iconLabel']").text
job_description = driver.find_element_by_xpath("//*[@class='jobsearch-jobDescriptionText']").text
编辑2:
为避免在某个作业没有公司名称时出错,可以执行以下操作:
try:
company_name = driver.find_element_by_xpath("//div[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']").text
except:
company_name = ""
希望有帮助。你会犯什么错误?@TeeKea我不会犯错误。我不知道怎么做?好吧,你需要找到你想收集的项目的类、标记或id名称,就像你获取可点击项目的方法一样。@TeeKea我可以在新页面上找到所需数据的类名。但是,如何使用类名和scrape?示例摘要项具有旧页面,selenium使用摘要项打开新窗口。单击()。但是如何用类名刮取新页面呢?对不起,如果我不清楚的话。谢谢你的帮助,请检查我的答案。谢谢。当我试图提取职务时,我遇到了这个错误“'WebElement'对象没有我看到的'page\u source'属性。您可以使用:
job\u title.text
Thank@TeeKea提取它。它起作用了。然而,这只是一个招聘广告,我必须为页面上的其他工作做些什么。我正在尝试添加循环,但它不起作用。我已更新了我的答案,以帮助您创建一个贯穿所有工作列表的循环。希望对您有所帮助。谢谢您的支持。它起作用了。然而,在其中一个循环中,我得到以下错误。“消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//div[@class='icl-u-lg-mr--sm icl-u-xs-mr--xs']”(会话信息:chrome=79.0.3945.130)“我正在搜索新西兰的数据科学家。共找到6页。我的代码在上面更新编辑