Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 利用网页抓取获取物品价格_Python_Web_Scrape_Price - Fatal编程技术网

Python 利用网页抓取获取物品价格

Python 利用网页抓取获取物品价格,python,web,scrape,price,Python,Web,Scrape,Price,我想制作一个Python脚本,但不幸的是,当我想检查价格时,我得到的是NONE,而不是价格本身(或者如果我更改代码,则得到US$00.00)。 我找到了很多例子,其中HTML带有id,但我不知道如何使用class 到目前为止,我所拥有的: 导入请求 从bs4导入BeautifulSoup URL='https://www.banggood.com/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3

我想制作一个Python脚本,但不幸的是,当我想检查价格时,我得到的是
NONE
,而不是价格本身(或者如果我更改代码,则得到US$00.00)。 我找到了很多例子,其中HTML带有
id
,但我不知道如何使用
class

到目前为止,我所拥有的:

导入请求
从bs4导入BeautifulSoup
URL='https://www.banggood.com/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_设计=-&fbclid=IwAR3cAovrze4opttuwwpUxnXZ6dkRjhau3RqmDqASPW0mSEOI8sJgn9Cqss8和分支机构匹配id=85039256428961100&cur仓库=CN&id=45764'
headers={“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/87.0.4280.88 Safari/537”}
page=requests.get(URL,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(“span”,“class”:'product-title-text'}).get_text()
price=soup.find(“span”,“class”:'main-price'}).get_text()
换算价格=价格[0:5]
打印(换算价格)
印刷品(标题)

当网站使用Javascript并动态更改时,BeautifulSoup
对网页抓取的作用非常小。现在大多数网站都使用Javascript,这使得抓取数据变得很困难。另一种选择是使用
Selenium

如果您已经使用了
Selenium
,则直接跳转到下面的代码块。如果没有,请按照以下说明操作

  • 检查您在选项菜单(浏览器右上角)下的关于Chrome的
    中使用的Chrome版本
  • 转到网站并下载相同版本的驱动程序
  • 创建文件夹
    C:\webdrivers
    ,并将下载的驱动程序复制到此文件夹中
  • 复制文件路径
    C:\webdrivers\chromedriver.exe
    并将其添加到
    环境变量中的路径中
    (
  • 现在执行下面的代码:

    from selenium import webdriver
    
    opts = webdriver.ChromeOptions()
    # The below line will make your browser run in background when uncommented
    # opts.add_argument('--headless')
        
    driver = webdriver.Chrome(options= opts, executable_path = 'C:\webdrivers\chromedriver.exe')
    
    driver.get("https://www.banggood.in/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_design=-&_branch_match_id=850392564428961100&cur_warehouse=CN&ID=45764")
    
    prices = driver.find_elements_by_class_name('main-price')
    
    for price in prices:
        print(price.text)
    driver.quit()
    
    输出:

    ₹1,712.63
    

    有关
    Selenium
    的更多详细信息,请浏览,这是一个令人印象深刻的模块!

    请提供一个HTML示例,并解释您要提取哪些部分。该商品现在更便宜了,我需要26.88美元26.88美元49.99-46%谢谢你的更新,但请代之以你的问题。非常感谢你的快速帮助和详细的答案!