繁体   English   中英

从 HTML 中提取/解码 CSS 到 Python

[英]Extract/decode CSS from HTML into Python

大家下午好。

我目前正在解析这个网站: http : //uk.easyroommate.com/results-room/loc/981238/pag/1

我想获得每个广告的每个 url 的列表。 然而,这个清单是用 JavaScript 编码的。 我可以通过 Firefox firebug 完美地看到它们,但我还没有找到任何通过 Python 获取它们的方法。 我认为这是可行的,但我不知道如何。

编辑:显然我已经尝试过像 BeautifulSoup 这样的模块,但因为它是一个 JavaScript 生成的页面,所以它完全没用。

预先感谢您的帮助。

广告列表由JavaScript生成。 BeautifulSoup 为您提供以下示例:

<ul class="search-results" data-bind="template: { name: 'room-template', foreach: $root.resultsViewModel.Results, as: 'resultItem' }"></ul>

我建议查看: Getting html source when some html is generated by javascript and Python Scraping JavaScript using Selenium and Beautiful Soup

感谢您的领导,这是解决方案,我希望有一天它会帮助某人:

from selenium import webdriver  
from bs4 import BeautifulSoup

browser = webdriver.Firefox()  
browser.get('http://uk.easyroommate.com/results-room/loc/981238/pag/1')  
html_source = browser.page_source  
browser.quit()

soup = BeautifulSoup(html_source,'html.parser')  
print soup.prettify()
## You are now able to see the HTML generated by javascript code and you 
## can extract it as usual using BeautifulSoup

for el in soup.findAll('div', class_="listing-meta listing-meta--small"):
    print el.find('a').get('href')

再次在我的情况下,我只想提取这些链接,但是一旦您通过 Selenium 获得了网页源代码,使用 beautifulSoup 并获得您想要的每一项都是小菜一碟。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM