鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Python/ Python 爬蟲,<span> ...</span>中

Python 爬蟲,<span> ...</span>中間的京東商品價格

如題目所示,頁面信息如下可以獲得商品名稱,但是價格是空的,用了好幾種方法。

圖片描述

import requests
from lxml import etree
from lxml import html


import urllib.request
from bs4 import BeautifulSoup

url = "https://item.jd.com/5056201.html"
page = requests.get(url)
tree = html.fromstring(page.text)
selector = etree.HTML(page.text)

ProductNameP = '/html/body/div[5]/div/div[2]/div[1]/text()'
Name = selector.xpath(ProductNameP) #獲得字符串
print(Name)

ProductPriceP ='/html/body/div[5]/div/div[2]/div[3]/div/div[1]/div[2]/text()'
Price = selector.xpath(ProductPriceP)
回答
編輯回答
尐潴豬

xpath該這么寫:"http://span[@class='price J-p-5056201']/text()|//span[@class='price J-p-p-5056201']/text()"

2018年2月22日 16:20
編輯回答
詆毀你

你可以利用瀏覽器的F12監(jiān)控network下的請求,可以找到價格的請求,不同商品的價格是利用不同的id請求的,所以...

2017年4月8日 19:45
編輯回答
局外人

商品的價格是通過js請求接口渲染的

2017年4月18日 06:01
編輯回答
念初

直接selenium+bs4解析多爽,干嘛還用這個呢

2018年2月7日 12:01
編輯回答
我不懂
price = selector.xpath("http://span[@class='price']/text()")

是 JS 渲染的

抓這個鏈接

2018年2月25日 10:18