从网页中提取信息

时间:2014-05-14 07:55:02

标签: python selenium

请你能帮助我如何使用python从以下方面获取移动模型及其价格。 我想从页面中提取名称Moto E(黑色)和Rs.6999。我在Python中尝试使用selenium(我是selenium的初学者)。这是我的代码。请帮帮我。

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.kart123.com/mobiles/pr?p%5B%5D=sort%3Dfeatured&sid=tyy%2C4io&ref=68c7d088-ae7f-4310-aa4c-a7ee176d168d")
elem=driver.find_element_by_xpath("//div[@class='product-unit unit-4 
browse-product']")
elem1=elem.find_element_by_xpath("//div[@class='pu-details lastUni']")
elem2=elem1.find_element_by_xpath("//div[@class='pu-title
fk-font-13']") print
elem2.find_element_by_xpath(".//a[@class='fk-display-block']").text<br>
driver.close()

<div class=' product-unit unit-4  browse-product  ' data-pid="MOBDVHC6XKKPZ3GZ" data-tracking-products=";MOBDVHC6XKKPZ3GZ;1;6999;;eVar22=Mobile" data-size="store-grid-new-4">
    <div class='pu-visual-section'>
        <a data-tracking-id="prd_img"  class='pu-image fk-product-thumb ' href="/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ&srno=b_1&ref=83c37824-b74d-4121-8be0-27731ddccde2">
        <img alt="Moto E: Mobile" data-error-url="http://img1a.flixcart.com/mob/thumb/mobile.jpg" onload="img_onload(this);" onerror="img_onerror(this);" src="http://img5a.flixcart.com/image/mobile/3/g/z/motorola-xt1022-125x125-imadvvfknshcywk5.jpeg"></img>
        </a>
    </div>
    <div class="pu-details lastUnit">
        <div class="pu-title fk-font-13">
            <a class="fk-display-block" data-tracking-id="prd_title" href="/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ&srno=b_1&ref=83c37824-b74d-4121-8be0-27731ddccde2" title="*Moto E (Black)*">
            Moto E (Black)
            </a>
        </div>
        <div class='pu-variants  fk-font-11'>
            and <a href="/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ&srno=b_1&ref=83c37824-b74d-4121-8be0-27731ddccde2">1 more variant</a>
        </div>
        <div class="pu-extra fk-font-11">
        </div>
        <div class="pu-rating" data-ratingfor="ITMDVUWSYBGNBTHA#MOBDVHC6XKKPZ3GZ#moto-e">
            <div class='fk-stars-small' title ='4.7 stars'>
                <div class='rating' style='width:94%;'>
                </div>
            </div>
            (852 ratings)<span class="ugc-summary-icon"></span>
        </div>
        <div class="pu-price">
            <div class="pu-border-top">
                <div class="pu-final">
                    <span class="fk-font-17 fk-bold">**Rs. 6999**</span>
                </div>
                <div class="pu-emi fk-font-12">EMI from Rs. 626</div>
                <div class="pu-personal">
                </div>
                <ul class="pu-offers">
                </ul>
            </div>
        </div>
        <div class="pu-border-top">
            <ul class="pu-usp">
                <li><span class="text">Android v4.4 OS</span></li>
                <li><span class="text">4.3-inch Touchscreen</span></li>
                <li><span class="text">1 GB RAM</span></li>
                <li><span class="text">Dual SIM (GSM + GSM)</span></li>
            </ul>
        </div>
        <div class="pu-compare pu-border-top">
            <input type="checkbox" class="compare-checkbox" data-uniqid="83c37824-b74d-4121-8be0-27731ddccde2" id="MOBDVHC6XKKPZ3GZ" display_vertical='Mobiles' vertical="mobile"  vertical_url_map='/mobiles'><label for="MOBDVHC6XKKPZ3GZ" class="compare-label">Add to compare</label>
        </div>
    </div>
</div>
</div>
<div class="gd-col gu3">

1 个答案:

答案 0 :(得分:0)

有一些工具用于您正在尝试做的事情。

Scrapy http://doc.scrapy.org)是编写网络抓取工具并使您保持数据最新的绝佳工具。您可以使用XPath表示法来访问数据(例如div[@class='pu-final']/ span/text()将为您提供Rs.6999)。

如果您不是所有Scrapy的功能并且不需要性能(如一次性导入脚本),那么还有 BeautifulSoup ({{ 3}})这很简单易用。

这些只是您可以使用的众多工具中的两个,但它们的记录非常好。我相信很多人都可以推荐一些其他很棒的工具,根据最符合您需求的方式做出选择。

祝你好运。

相关问题