【问题标题】:Scraping airbnb with python lxml使用 python lxml 抓取 airbnb
【发布时间】:2016-04-20 01:50:00
【问题描述】:

我正在尝试在 airbnb 列表中查找节点。节点是

< div class="col-md-3 text-muted" data-reactid=".2e7if3twveo.0.0.0.0.1.6.0">< span data-reactid=".2e7if3twveo.0.0.0.0.1.6.0.0">The Space< /span> /div> 

import mechanize

br = mechanize.Browser()

url ='https://www.airbnb.com/rooms/5711344'
tree = html.fromstring(br.open(url).get_data())
els = tree.xpath('//div[@class="row"]/div[@class="col-md-3 text-muted"]')
for element in els:
    if element.text.find('The Space') >= 0:

不知何故,“空间”是不可检索的。

【问题讨论】:

  • 这是复制粘贴错误吗? &lt; /span&gt; /div&gt; /div 没有结束 &lt;

标签: python beautifulsoup lxml


【解决方案1】:

这对我有用:我使用 BeautifulSoup 通过类属性获取 div,然后循环寻找正确的。

import requests
from bs4 import BeautifulSoup

url = 'https://www.airbnb.com/rooms/5711344'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
divs = soup.find_all('div', attrs={'class': 'col-md-3 text-muted'})
for div in divs:
    space = div.find('span').text.strip()
    if space == "The Space":
        print(space)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多