简体   繁体   English

使用 requests.get 后,我不能 g

[英]After using requests.get, I cannot g

I am using python scrape to find if a LV handbag is in stock.我正在使用 python scrape 查找 LV 手提包是否有货。 The following is my code:以下是我的代码:

import requests
from lxml import etree


endpoint = 'https://us.louisvuitton.com/eng-us/products/graceful-pm-damier-azur-canvas-nvprod840045v'


headers = {
 'Connection': 'keep-alive',
 'Cache-Control': 'max-age=0',
 'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
 'sec-ch-ua-mobile': '?0',
 'Upgrade-Insecure-Requests': '1',
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
 'Sec-Fetch-Site': 'same-origin',
 'Sec-Fetch-Mode': 'navigate',
 'Sec-Fetch-User': '?1',
 'Sec-Fetch-Dest': 'document',
 'Referer': 'https://bj.ke.com/',
 'Accept-Language': 'zh-CN,zh;q=0.9',
}      # hearders
lv_response = requests.get(endpoint,headers=headers) # request to get
content_lv = lv_response.text # to text


html_lv = etree.HTML(content_lv) # using lxml to transform to html
stock_check = html_lv.xpath("//div[@class='lv-product__price-stock']/span/text()") # to locate Item Unavailable, Check Back Soon

print(stock_check) # the result gives me an empty list

As mentioned in the code, I first using inspect elements in the browser to find the key word: Item Unavailable, Check Back Soon with Xpath.如代码中所述,我首先使用浏览器中的检查元素来查找关键字:Item Unavailable,Check Back Soon with Xpath。 在此处输入图像描述

Then in the code, I also tried to locate the text Item Unavailable, Check Back Soon with Xpath again, but it will not give me any results.然后在代码中,我也尝试定位文本 Item Unavailable, Check Back Soon with Xpath again,但它不会给我任何结果。

I did some research.我做了一些研究。 The website may be using JavaScript and I should use Selenium, and I have not tried Selenium. I am wondering if there is any way to find the keywords with the code I have now?该网站可能使用JavaScript,我应该使用Selenium,我没有尝试过Selenium。我想知道是否有任何方法可以用我现在的代码找到关键字? Or please correct me if you can find any issues.或者,如果您发现任何问题,请纠正我。 Great thank!非常感谢!

So I would suggest you to use Selenium .所以我建议你使用Selenium The code become as simple as this:代码变得如此简单:

from selenium import webdriver
from time import sleep

dr = webdriver.Chrome()
dr.get("https://us.louisvuitton.com/eng-us/products/graceful-pm-damier-azur-canvas-nvprod840045v")

sleep(2)  # to load JS

stock_check  = dr.find_element_by_xpath("//div[@class='lv-product__price-stock']/span").text
print(stock_check)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM