Python 刮 BeautifulSoup

Python scrape with BeautifulSoup

您好,我正在尝试从此网页检索数据:

http://brmr.biz/product/2716370/name/000300020_30_2_DOORSLAG

我正在使用 Python 2.7.10 和 BeautifulSoup 库。

这是我的代码:

from BeautifulSoup
import BeautifulSoup
import urllib2

url="http://brmr.biz/product/2716370/name/000300020_30_2_DOORSLAG"

page=urllib2.urlopen(url)

soup=BeautifulSoup(page)

data = soup.findAll("span", {"class":"price_bigger"})

print data`

我想获得 5,90 的价值(价格不含税)?我的代码得到了结果 "request price"

我尝试 "stalling" 使用 time.sleep(1) 的代码到 "load" 数据,但这不起作用。

谢谢你帮助我

正如其他人在评论中指出的那样,price 动态加载了一个额外的 POST 请求,您需要在代码中模拟该请求。

这里我用的是requests to maintain a web-scraping session and beautifulsoup4:

from bs4 import BeautifulSoup
import requests

url = "http://be.brammer.biz/product/2716370/name/000300020_30_2_DOORSLAG"
price_url = "http://be.brammer.biz/products/show-price"

with requests.Session() as session:
    soup = BeautifulSoup(session.get(url).content)

    # extract the product code - used in the POST request
    product_code = soup.find(id="localitemid")["value"]

    response = session.post(price_url, data={
        "ids[0][]": product_code
    }, headers={
        "X-Requested-With": "XMLHttpRequest"
    })

    print(response.json())

打印包含价格的对象:

{u'bd792fcb87': [{u'delivery_time': u'6-10', u'quantity_min': 1, u'hash': u'bd792fcb87', u'qty_order_interval': 1, u'price': u'5,90\xa0\u20ac', u'tax': 0.21, u'delivery_desc': u'Aanvraag / Demande / Anfrage', u'price_unlocalized': 5.9, u'localitemid': u'2000010600', u'currency': u'\u20ac', u'quantity_available': 0, u'delivery_time_max': 10, u'quantity_interval': 1, u'price_quantity': 1, u'price_vat': u'7,14\xa0\u20ac'}]}