下面是我的代码,以获得产品名称"RENU新鲜镜头解决方案120 ML“从url..this躺在p标签.我只需要这个名字。
import requests
import lxml
from bs4 import BeautifulSoup
url = "http://www.lenskart.com/renu-fresh-lens-solution-100-ml.html"
source = requests.get(url)
data = source.content
soup = BeautifulSoup(data, "lxml")
pn = soup.find_all("div", {"class":"prcdt-overview"})[0].text
print pn发布于 2016-12-26 10:35:21
import requests
from bs4 import BeautifulSoup
url = "http://www.lenskart.com/renu-fresh-lens-solution-100-ml.html"
source = requests.get(url)
# data = source.content pass the variable in the BeautifulSoup()
soup = BeautifulSoup(source.content, "lxml")查找()版本:
pn = soup.find('div', class_="prcdt-overview").p.text'lxml',BeautifulSoup会帮您完成的find_all()的第一个标记,则应该尝试find(),它将在find_all()中返回第一个标记tag.tag.find()/find_all()逐步获取标记。tag.tag_name是tag.find('tag_name')的简称CSS选择器版本:
soup.select_one(".prcdt-overview p").textselect_one()将返回select()的第一个标记,如find()和find_all()发布于 2016-12-26 09:04:42
尝尝这个
pn = soup.select(".prcdt-overview h1[itemprop=name] p")[0].text或
pn =soup.select(".prcdt-overview")[0].select("h1[itemprop=name]>p")[0].text还有其他的方法,试试这些
希望这能有所帮助
发布于 2016-12-26 09:22:20
更详细的方式:
pn = soup.find_all("div", {"title":"prcdt-overview"})[0]
divTitle = pn.find("div",{"class":"title"})
pText = divTitle.find("p").text
print pTexthttps://stackoverflow.com/questions/41328544
复制相似问题