Python 美丽的乌苏不能得到一切

Python 美丽的乌苏不能得到一切,python,beautifulsoup,Python,Beautifulsoup,2周前,我可以阅读此url源代码中的所有内容: 然而,今天,当我再次运行相同的代码时,所有的历史价格都不能出现在汤中。。。。你知道如何解决这个问题吗 这是我的python代码(它工作得很好!) 我对解决方案不是很确定,但您通常应该避免使用太多的列表索引和find_all子句。原因是元素的位置或数量比类、ID等更容易改变。因此,我建议使用css选择器。通过阅读源代码,历史价格数据似乎是通过JavaScript访问的。因此,您需要找到一种模拟真实浏览器的方法。就我个人而言,我用它来完成这类任务 fr

2周前,我可以阅读此url源代码中的所有内容:

然而,今天,当我再次运行相同的代码时,所有的历史价格都不能出现在汤中。。。。你知道如何解决这个问题吗

这是我的python代码(它工作得很好!)


我对解决方案不是很确定,但您通常应该避免使用太多的列表索引和
find_all
子句。原因是元素的位置或数量比类、ID等更容易改变。因此,我建议使用css选择器。

通过阅读源代码,历史价格数据似乎是通过JavaScript访问的。因此,您需要找到一种模拟真实浏览器的方法。就我个人而言,我用它来完成这类任务

from bs4 import BeautifulSoup
from urllib2 import urlopen

url = 'http://camelcamelcamel.com/Jaybird-Sport-Wireless-Bluetooth-Headphones/product/B013HSW4SM?active=price_amazon'
soup = BeautifulSoup(urlopen(url),'html.parser')
lst = soup.find_all('tbody')
for tbody in lst:
    trs = tbody.find_all('tr')
    for elem in trs:
        tr_class = elem.get('class')
        if tr_class != None:
            if tr_class[0] == 'highest_price' or tr_class[0] == 'lowest_price':
                tds = elem.find_all('td')
                td_label = tds[0].get_text().split(' ')[0]
                td_price = tds[1].get_text()
                td_date = tds[2].get_text()
                print td_label, td_price, td_date
        else:
            tds = elem.find_all('td')
            td_label = tds[0].get_text().split(' ')[0]  
            if td_label == 'Average':
                td_price = tds[1].get_text()
                print td_label, td_price  

ps = soup.find_all('p')
for p in ps:
    p_class = p.get('class')
    if p_class != None and len(p_class) == 2 and p_class[0] == 'smalltext' and p_class[1] == 'grey':
        p_text = p.get_text()
        m = re.search('since([\w\d,\s]+)\.', p_text)
        if m:
            date = m.group(1)
            dt = datetime.datetime.strptime(date, ' %b %d, %Y')
            print datetime.date.strftime(dt, '%Y-%m-%d')
        break