如何获取锚标记内的元素?

时间:2018-07-19 08:55:11

标签: python selenium selenium-webdriver web-scraping web-crawler

对不起,我对使用Selenium和python进行网络抓取非常陌生。我正在尝试抓取html中具有以下部分的超级市场网站的内容

<div class="itemDescription">
            <meta itemprop="priceCurrency" content="INR">
            <meta itemprop="price" content="23.00">
        <h4 class=""><strong class="price js-effective-mrp" data-currency="₹">₹ 23.00 </strong>
                                    <s class="js-actual-mrp" style="display:none;"></s>
                                <br><a href="/fresh-onion-red-v-1-kg-p.php" class="">Fresh Onion Red <span class="item-quantity">1 Kg</span></a></h4>
                    </div>

我需要产品的价格,数量和名称。

下面是我编写的代码,但是没有正确解析元素。

div = driver.find_element_by_class_name('itemDescription')
sname =div.find_element_by_css_selector('a').get_attribute('href')
squantity =driver.find_elements_by_class_name('item-quantity')
sprice = driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "js-effective-mrp", " " ))]')

请帮助

3 个答案:

答案 0 :(得分:1)

尝试以价格购买xPath:

import React from "react";
import App from "../App";

class UserAuthenticationUI extends React.Component {
  constructor(props) {
    super(props);
    this.app = React.createRef();
    this.onClick = this.onClick.bind(this);
  }
  onClick(){
      const newStateValue = 'new parent state value';
      if(typeof this.props.setParentState !== 'undefined'){
        this.props.setParentState(newStateValue);
      }
  }
  render() {
    const stateProps = this.props.stateVariable;
    return (
      <div>
        <App ref={this.app} />
        <div onClick={this.onClick} />
        {console.log(stateProps)}
      </div>
    );
  }
}

export default UserAuthenticationUI;

或者如果您想要所有货币:

//strong[@class='price js-effective-mrp' and @data-currency='₹']

此链接:

//strong[@class='price js-effective-mrp']

这是数量:

//div[@class='itemDescription']//a

示例:

//span[@class = 'item-quantity']

根据您的反馈,您不能从列表中获取文本,但是可以从列表中的每个元素中获取文本,如下所示:

sname = driver.find_element_by_xpath("//div[@class='itemDescription']//a")
squantity = driver.find_element_by_xpath("//span[@class = 'item-quantity']")
sprice = driver.find_element_by_xpath("//strong[@class='price js-effective-mrp' and @data-currency='₹']")

print(squantity.text) # prints quantity
print(sname.text) # prints name
print(sprice.text) # prints price

答案 1 :(得分:0)

为什么不尝试使用Synx的Xcuse这种逻辑,

String strProductDescription = driver.find_element_by_class_name('itemDescription').getText();
String arrString = strProductDescription.split("SomeDelimeter");

答案 2 :(得分:0)

对于 MRP ,您可以尝试以下操作:

sprice = driver.find_element_by_css_selector('strong.price.js-effective-mrp') 

然后您可以使用.text方法提取文本:

print(sprice.text)  

数量

squantity = driver.find_element_by_css_selector('span.item-quantity')
print(squantity.text) 

姓名:

sname = driver.find_element_by_xpath("//div[@class='itemDescription']//a")
print(sname.text) 

请注意,使用以下xpath://div[@class='itemDescription']//a将得到此Fresh Onion Red 1 Kg作为输出。

基本上新鲜洋葱红是一个文本节点。

相关问题