【发布时间】:2019-02-05 16:58:39
【问题描述】:
我正在尝试使用 python 3 库从 AirBnb 的城市页面中提取列表的 URL。我熟悉如何使用 Beautifulsoup 和 requests 库抓取更简单的网站。
网址:'https://www.airbnb.com/s/Denver--CO--United-States/homes'
如果我检查页面上的链接元素(在 Chrome 中),我会得到:
xpath: "//*[@id="listing-9770909"]/div[2]/a"
selector: "listing-9770909 > div._v72lrv > a"
我的尝试:
import requests
from bs4 import BeautifulSoup
url = 'https://www.airbnb.com/s/Denver--CO--United-States/homes'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
divs = soup.find_all('div', attrs={'id': 'listing'})
尝试2:
import requests
from lxml import html
page = requests.get(url)
root = html.fromstring(page.content)
tree = root.getroottree()
result = root.xpath('//div[@id="listing-9770909"]/div[2]/a')
for r in result:
print(r)
这些都不返回任何东西。我需要能够提取的是页面链接的 url。有什么想法吗?
【问题讨论】:
-
检查 DOM 是不够的,您需要查看源代码并在页面加载时验证它是否存在。因为这些库都没有 javascript 引擎。
标签: python xpath web-scraping beautifulsoup lxml