【发布时间】:2020-02-19 20:58:29
【问题描述】:
我正在学习使用 Python 使用 Selenium 网络驱动程序进行网络抓取。为了我的学习目的,我正在抓取 Indeed.com。我正在抓取职位、公司名称、地点、薪水和工作摘要。我可以使用漂亮的汤提取职位、公司名称、位置、薪水。工作摘要正在加载到我尝试使用 selenium 提取数据但不成功的下一页。我已经检查了这里的所有帖子,但仍然无法做到。我可以点击新页面,但我不确定如何从新页面中抓取数据。
我的代码
#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?
我可以点击新页面,但我不确定如何从新页面中抓取数据。我也需要自动为其他页面做。有什么建议吗?
【问题讨论】:
-
遇到什么错误?
-
@TeeKea 我没有收到错误。不知道怎么弄?
-
嗯,你需要找到你想要收集的项目的类、标签或 ID 名称,就像获取可点击项目一样。
-
@TeeKea 我可以在新页面上找到所需数据的类名。但是,如何使用类名和抓取?示例摘要项具有旧页面,并且 selenium 使用 summaryItem.click() 打开新窗口。但是如何用类名抓取新页面呢?对不起,如果我不清楚。感谢您的帮助
-
请检查我的回答。
标签: python-3.x selenium selenium-webdriver