【问题标题】:Scraping With Selenium, PhantomJS & BS4使用 Selenium、PhantomJS 和 BS4 进行抓取
【发布时间】:2020-09-04 22:10:26
【问题描述】:

我目前使用的是 Windows 10 和 Python 3.7,并且我一直在阅读有关如何在不为 urls 列表中被抓取的每个 URL 打开 1 个 Firefox 浏览器窗口的情况下进行抓取的信息。下面的代码正在抛出一个错误,我确信它与 PhantomJS 的实现方式有关,我只是不知道具体是什么。

我听说 PhantomJS 在与 Selenium 一起使用时是一种解决方案。我安装了 PJS,在我的计算机上设置了路径,它似乎正在运行,但是我不完全确定如何在代码本身中实现它。

driver = webdriver.PhantomJS(executable_path=r"C:\phantomjs") 行是试图运行 PJS 的行。代码在使用 driver = webdriver.Firefox() 之前运行良好。

urls = ["https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=0&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=90&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=180&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=270&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=360&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=450&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=540&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=630&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=720&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=810&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=900&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD","https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=990&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD"]
#url = "https://www.guitarcenter.com/Used/Bass.gc#pageName=used-page&N=18171+1076&Nao=180&recsPerPage=90&postalCode=02494&radius=100&profileCountryCode=US&profileCurrencyCode=USD"

user_agent = UserAgent()

#make csv file
csv_file = open("gcscrape.csv", "w", newline='') #added the newline thing on 5.17.20 to try to stop blank lines from writing
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["bass_name","bass_price"])

for url in urls:
    web_r = requests.get(url)
    web_soup = BeautifulSoup(web_r.text,"html.parser")

        #print(web_soup.findAll("li", class_="product-container")) #finding all of the grid items on the url above - price, photo, image, details and all
        #print(len(web_soup.findAll("li", class_="product-container"))) #printing out the length of the

    #driver = webdriver.Firefox()
    driver = webdriver.PhantomJS(executable_path=r"C:\phantomjs")
    driver.get(url)
    html = driver.execute_script("return document.documentElement.outerHTML") #whats inside of this is a javascript call to get the outer html content of the page
    sel_soup = BeautifulSoup(html, "html.parser")

    for content in sel_soup.findAll("li", class_="product-container"):
            #print(content)

        bass_name = content.find("div", class_="productTitle").text.strip() #pulls the bass guitar name
        print(bass_name)

        prices_new = []
        for i in content.find("span", class_="productPrice").text.split("$"):
            prices_new.append(i.strip())
        bp = prices_new[1]
        print(bp)

        #write row to new csv file
        csv_writer.writerow([bass_name, bp])

【问题讨论】:

  • 您好,使用PhantomJS有什么要求吗?我问是因为它现在已被弃用。更多信息在这里。 phantomjs.org。 Firefox 也有无头模式和 chrome。正如您所说,它适用于 Firefox,这里有一些关于如何在 Firefox 中运行无头模式的说明。 stackoverflow.com/questions/46753393/…
  • 我看不到,没有。我找到的所有教程都只是去 PhantomJS 的网站,下载适用于 Windows 的版本,在我的电脑 > 高级系统设置下在你的计算机上设置 PJS 的路径,你应该都设置好了。无头运行 Firefox 的能力是否一直存在?或者 PJS 贬值后 Selenium 包含的东西?
  • 我不确定,但是自从 Chrome 和 Firefox 发布了无头模式后,PhantomJS 项目开始变得过时了,开发人员只是停止使用它来使用 Chrome 或 Firefox 无头模式。

标签: python selenium phantomjs


【解决方案1】:

确保为您的操作系统here下载正确的 PhantomJs 发行版。

对于 Windows,以下代码行应该可以正常工作:

driver = webdriver.PhantomJS("C://phantomjs.exe")
driver.get(url)

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2019-02-12
    • 2014-03-18
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多