启动刮板后,我没有得到一个输出

2024-04-25 07:06:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个scraper来检索产品名称、Cat No、大小和价格,但是当我运行脚本时,它不会给我一个输出或错误消息。我用的是Jupyter笔记本,不确定这是不是问题。我也不确定,因为我是输入到一个CSV文件,如果这也是给它的问题。任何帮助都将不胜感激。你知道吗

这是我正在运行的代码。你知道吗

from selenium import webdriver
import csv, os
from bs4 import BeautifulSoup

os.chdir(r'C:\Users\kevin.cragin\AppData\Local\pip\Cache\wheels\09\14\7d\1dcfcf0fa23dbb52fc459e5ce620000e7dca7aebd9300228fe') 
driver = webdriver.Chrome()
driver.get('https://www.biolegend.com/en-us/advanced-search?GroupID=&PageNum=1')
html = driver.page_source

containers = html.find_all('li', {'class': 'row list'})

with open("BioLegend_Crawl.csv", "w") as f:

    f.write("Product_name, CatNo, Size, Price\n")

    for container in containers:

        product_name = container.find('a',{'itemprop':'name'}).text
        info = container.find_all('div',{'class':'col-xs-2 noPadding'})
        catNo = info[0].text.strip()
        size = info[1].text.strip()
        price = info[2].text.strip()

        print('Product_name: '+ product_name)
        print('CatNo: ' + catNo)
        print('Size: ' + size)
        print('Price: ' + price + '\n')

        f.write(','.join([product_name,catNo,size,price]))

Tags: textnamefromimportinfosizecontainerdriver
1条回答
网友
1楼 · 发布于 2024-04-25 07:06:04

你使用的网站从技术上讲是从数据库加载信息,因此它不是预设在网站HTML中默认加载哪些产品名称。它们必须根据搜索约束动态加载。你知道吗

所以你需要下载chromedriver.exe文件(如果您使用Google Chrome)或其他自动运行web浏览器的驱动程序(PhantomJS是另一个好的驱动程序),那么您需要在计算机上指定此.exe所在的路径位置,如下所示:

import selenium import webdriver
import csv, os
from bs4 import BeautifulSoup

os.chdir('Path to chromedriver or other driver') 
driver = webdriver.Chrome()
driver.get('Link to your webpage you want to extract HTML from')
html = driver.page_source
soup = BeautifulSoup(html)

containers = soup.find_all('ul',{'id':'productsHolder'})

with open("BioLegend_Crawl.csv", "w") as f:

    f.write("Product_name, CatNo, Size, Price\n")

    for container in containers:

        product_name = container.find('a',{'itemprop':'name'}).text
        info = container.find_all('div',{'class':'col-xs-2 noPadding'})
        catNo = info[0].text.strip()
        size = info[1].text.strip()
        price = info[2].text.strip()

        print('Product_name: '+ product_name)
        print('CatNo: ' + catNo)
        print('Size: ' + size)
        print('Price: ' + price + '\n')

        f.write(','.join([product_name,catNo,size,price]))

相关问题 更多 >