Python网络抓取表

时间:2018-06-27 21:01:14

标签: python html python-2.7 web-scraping

我正在尝试使用pythin从网站上抓取一些数据。该网站包含许多不同的锻炼,每个锻炼都有自己的数据。我已经弄清楚了如何从每个特定的锻炼中抓取数据,但是要做到这一点,我必须在url中提供一个特定的锻炼ID。主页显示为在表格中列出所有这些锻炼ID,但是当我使用以下方法搜索html文档时 漂亮的汤返回以下表数据:

<table class="table table-striped table-hover">
<thead>
<tr>
<th ng-click="order('class_name')" style="cursor:pointer;">Name</th>
<th ng-click="order('location')" style="cursor:pointer;">Location</th>
<th ng-click="order('trainer')" style="cursor:pointer;">Instructor</th>
<th ng-click="order('class_date_sec')" style="cursor:pointer;">Date</th>
<th ng-click="order('points')" style="cursor:pointer;">OT Points</th>
<th ng-click="order('CALORIES')" style="cursor:pointer;">Total Calories 
(kCal)</th>
</tr>
</thead>
<tbody id="otf-class-body">
<tr calories="{{class.CALORIES | number:0}}" class_date="{{class.class_date}} 
    at {{class.class_time}}" class_name="{{class.class_name}}" date_order=" 
    {{class.date_order}}" id="{{class.CLASSID}}" loc="{{class.loc}}" 
    location=" {{class.location}}" ng-click="view(class.CLASSID, 
    class.at_home)" ng-repeat="class in classes | orderBy:predicate:reverse" 
    points=" {{class.points | number:0}}" trainer="{{class.trainer}}">
<td>{{class.class_name}}</td>
<td>{{class.location}}</td>
<td>{{class.trainer}}</td>
<td>{{class.class_date}} at {{class.class_time}}</td>
<td>{{class.points | number:0}}</td>
<td>{{class.CALORIES | number:0}}</td>
</tr>
</tbody>
</table>

如您所见,没有实际的文本,相反,所有信息似乎都是某种变量(我的html知识非常有限)。看来我想要的信息将是所有信息的列表:

class.CLASSID

是否可以使用python获取此信息?或者使用某些我无法访问的api。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好伙伴:)我认为这可以工作,但是可以使用python3.x 希望对您有帮助

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

options =webdriver.ChromeOptions()
options.add_argument('headless')
# If you want it headless uncomment the line underneath and comment  out driver = webdriver.Chrome()
# driver = webdriver.Chrome(chrome_options = options)
driver = webdriver.Chrome()
url = ('https://carmel.orangetheoryfitness.com/login')
driver.get(url)
inputElement = driver.find_element_by_id("email")
inputElement.send_keys("YOUR EMAIL HERE")#put your email between the ""s
inputElement = driver.find_element_by_id("password")
inputElement.send_keys("YOUR PASSWORD HERE")#put your password between the ""s
inputElement.send_keys(Keys.ENTER)
driver.get("carmel.orangetheoryfitness.com/apps/otf/classes")
html = driver.page_source
print(html)

回顾

因为它希望您登录,所以我知道的解决方案是, 与硒有关。 可以肯定的是,还有其他方法,我希望大家能分享一下:) 我建议您无头使用它,因为它的混乱程度较小,浏览器将在后台运行,但对于调试,请像这样使用, 一旦您准备好使用该代码,只需取消注释即可,一切都会像魅力一样起作用,我希望我能帮助您配合!随时为任何问题加油

启用代码!

相关问题