从HTML中提取/解码CSS到Python

2024-06-26 13:47:35 发布

您现在位置:Python中文网/ 问答频道 /正文

大家下午好。在

我当前正在分析此网站:http://uk.easyroommate.com/results-room/loc/981238/pag/1。在

我想得到每个广告的每个网址清单。但是这个列表是用JavaScript编写的。我可以通过firefoxfirebug完美地看到它们,但是我还没有找到任何方法通过Python获得它们。我认为这是可行的,但我不知道怎么做。在

编辑:显然,我试过像beauthoulsoup这样的模块,但由于它是JavaScript生成的页面,因此完全没有用。在

提前谢谢你的帮助。在


Tags: 方法comhttp列表网站javascriptresultsloc
2条回答

广告列表由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和{a2}。在

多亏了你的领导,这才是解决问题的办法,我希望有一天它会对某些人有所帮助:

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获得了web页面源代码,那么使用beauthoulsoup并获得所需的每一项都是小菜一碟。在

相关问题 更多 >