【发布时间】:2016-03-21 08:05:44
【问题描述】:
这是对问题的完整编辑,因为根据答案,我的问题肯定问得不好 - 所以我会尽量说得更清楚。
我有一个想要抓取的对象。在我的笔记本电脑上使用的代码中,我可以毫无问题地让它工作。当我转移到 Pythonanywhere 时,我再也无法获得我正在寻找的信息。
在我的系统上运行的代码是:
from urllib.request import urlopen
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import csv
import time
import re
#68 lines of code for another section of the site above this working well on my system and on pythonanywhere.
pageSource = driver.page_source
bsObj = BeautifulSoup(pageSource)
try:
parcel_number = bsObj.find(id="mParcelnumbersitusaddress_mParcelNumber")
s_parcel_number =parcel_number.get_text()
except AttributeError as e:
s_parcel_number = "Parcel Number not found"
# same kind of code (all working) that gets 10 more pieces of data
# Tax Year
try:
pause = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "TaxesBalancePaymentCalculator")))
taxes_owed_2015_yr = bsObj.findAll(id="mGrid_RealDataGrid")[1].findAll('tr')[1].findAll('td')[0]
except IndexError as e:
s_taxes_owed_2015_yr = "No taxes due"
此代码在我的带有 fireforx 的笔记本电脑上运行良好 - 在 Pythonanywhere 上,如果我打印我要抓取的页面的页面源,我会在我的表格应该位于的位置得到以下信息:
<table border="0" cellpadding="5" cellspacing="0" class="WithBorder" width="100%">
<tbody><tr>
<td id="TaxesBalancePaymentCalculator"><!--DONT_PRINT_START-->
<span class="InputFieldTitle" id="mTabGroup_Taxes_mTaxChargesBalancePaymentInjected_mReportProcessingNote">Please wait while your current taxes are calculated.</span><img src="images/progress.gif"/> <!--DONT_PRINT_FINISH--></td>
</tr> <!--DONT_PRINT_START-->
<script type="text/javascript">
function TaxesBalancePaymentCalculator_ScriptLoaded( pPageContent )
{
element('TaxesBalancePaymentCalculator').innerHTML = pPageContent;
}
function results_ready()
{
element('pay_button_area').style.display = 'block';
element('pay_button_area2').style.display = 'block';
element('pay_additional_things_area').style.display = 'block';
}
var no_taxes_calculator = '&nbsp;<' + 'span class="MessageTitle">The tax balance calculator is not availab
le.<' + '/span>';
function no_taxes_calculator_available()
{
element('TaxesBalancePaymentCalculator').innerHTML = no_taxes_calculator;
}
function invalid()
{
element('TaxesBalancePaymentCalculator').innerHTML = no_taxes_calculator;
}
loadScript( 'injected/TaxesBalancePaymentCalculator.aspx?parcel_number=15-720-01-01-00-0-00-000' );
</script><script id="injected_taxesbalancepaymentcalculator_ScriptTag" type="text/javascript"></script>
<tr id="pay_button_area" style="DISPLAY: none">
<td id="pay_button_area2">
<table border="0" cellpadding="2" cellspacing="0">
<tbody><tr>
我玩过,发现如果我得到innerHTML(作为str):
element('TaxesBalancePaymentCalculator').innerHTML = pPageContent;
该部分保存我的数据 - 问题是我无法在字符串上执行 findAll 并且我需要表中的某些行:
taxes_owed_2015_yr = bsObj.findAll(id="mGrid_RealDataGrid")[1].findAll('tr')[1].findAll('td')[0]
我需要有关如何将该元素作为对象(而不是字符串)获取的帮助,以便我可以在我的数据中使用它。我已经尝试了很多东西,我无法在这里一一列举。我真的可以请一些帮助。
提前致谢。
【问题讨论】:
-
我不记得
Python中的任何findAll方法。这是bs4方法...在您的代码中导入bs4吗?你想用bsObj做什么? -
是的,它是一种 bs4 方法,我已经导入了 bs4——高了几百行。我正在尝试从内部 HTML 中的表格中获取信息--
-
根据文档,driver.get_attribute 返回一个字符串,因此出现错误。
-
@Raymond,恐怕
bs4模块的工作方式有点不同......你应该多读一些crummy.com/software/BeautifulSoup/bs4/doc
标签: python html selenium beautifulsoup html-parsing