0

在这方面遇到了很多麻烦......如果我只是不知道正确的搜索词来自己查找信息,那么对 Python 来说很抱歉。我什至不肯定这是因为 JS,但这是我有的最好的主意。

这是我正在解析的 HTML 部分:

...
<div class="promotion">
    <div class="address">
        <a href="javascript:PropDetail2('57795471:MRMLS')" title="View property detail for 5203 Alhama Drive">5203 Alhama Drive</a>
    </div>
</div>
...

...以及我用来做这件事的 Python(这个版本是我最接近成功的版本):

homeFinderSoup = BeautifulSoup(open("homeFinderHTML.html"), "html5lib")
addressClass = homeFinderSoup.find_all('div', 'address')
for row in addressClass:
    print row.get('href')

...返回

None
None
None
4

1 回答 1

0
# Create soup from the html. (Here I am assuming that you have already read the file into
# the variable "html" as a string).
soup = BeautifulSoup(html) 
# Find all divs with class="address"
address_class = soup.find_all('div', {"class": "address"})
# Loop over the results
for row in address_class:
  # Each result has one <a> tag, and we need to get the href property from it.
  print row.find('a').get('href')
于 2012-05-29T18:03:56.943 回答