Python 数据抓取 - 基本概念

标签 python html screen-scraping

当您查看 HTML(即 DOM 抓取)时,我试图了解数据抓取的工作原理。

我一直在尝试编写一个简单的 Python 代码来自动检索看过特定广告的人数:其中显示“本周有 3365 人浏览了 Peter 的房源”的部分。

起初我试图查看 HTML 代码中是否显示了该内容,但没有找到。做了一些研究,发现并非所有内容都在代码中,因为浏览器可以通过 JavaScript 或我不太理解的其他语言来处理它。然后我检查了该元素并意识到我需要使用 Python 库“retrieve”和“lxml.html”。所以我写了这段代码:

import requests
import lxml.html

response = requests.get('https://www.airbnb.co.uk/rooms/501171')
resptext = lxml.html.fromstring(response.text)
final = resptext.text_content()
finalu = final.encode('utf-8')

file = open('file.txt', 'w')

file.write(finalu) 

file.close()

这样,我就得到了包含网页中所有文本的代码,但不是我要查找的文本!这是神奇的数字 3365。

所以我的问题是:我如何获得它?我想也许我没有使用正确的语言来获取 DOM,也许它是用 JavaScript 完成的,而我只使用了 lxml。不过,我不知道。

最佳答案

您正在查看的 DOM 元素在页面加载后会更新,看起来像是带有以下请求 URL 的 AJAX 调用:

https://www.airbnb.co.uk/rooms/501171/personalization.json

如果您获取该 URL,它将返回以下 JSON 数据:

{
   "extras_price":"£30",
   "preview_bar_phrases":{
      "steps_remaining":"<strong>1 step</strong> to list"
   },
   "flag_info":{

   },
   "user_is_admin":false,
   "is_owned_by_user":false,
   "is_instant_bookable":true,
   "instant_book_reasons":{
      "within_max_lead_time":null,
      "within_max_nights":null,
      "enough_lead_time":true,
      "valid_reservation_status":null,
      "not_country_or_village":true,
      "allowed_noone":null,
      "allowed_everyone":true,
      "allowed_socially_connected":null,
      "allowed_experienced_guest":null,
      "is_instant_book_host":true,
      "guest_has_profile_pic":null
   },
   "instant_book_experiments":{
      "ib_max_nights":14
   },
   "lat":51.5299601405844,
   "lng":-0.12462748035984603,
   "localized_people_pricing_description":"&pound;30 / night after 2 guests",
   "monthly_price":"&pound;4200",
   "nightly_price":"&pound;150",
   "security_deposit":"",
   "social_connections":{
      "connected":null
   },
   "staggered_price":"&pound;4452",
   "weekly_price":"&pound;1050",
   "show_disaster_info":false,
   "cancellation_policy":"Strict",
   "cancellation_policy_link":"/home/cancellation_policies#strict",
   "show_fb_cta":true,
   "should_show_review_translations":false,
   "listing_activity_data":{
      "day":{
         "unique_views":226,
         "total_views":363
      },
      "week":{
         "unique_views":3365,
         "total_views":5000
      }
   },
   "should_hide_action_buttons":false
}

如果您查看“listing_activity_data”,您将找到您想要的信息。将 /personalization.json 附加到任何房间 URL 似乎都会返回此数据(目前)。

根据用户代理问题进行更新

看起来他们正在根据用户代理过滤对此 URL 的请求。我必须在 urllib 请求上设置用户代理才能解决此问题:

import urllib2
import json


headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request('http://www.airbnb.co.uk/rooms/501171/personalization.json', None, headers)
json = json.load(urllib2.urlopen(req))

print(json['listing_activity_data']['week']['unique_views'])

关于Python 数据抓取 - 基本概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015425/

相关文章:

python - Ghost.py 找不到 PySide?

javascript - 无法获取 CasperJS 的链接

python - 仅当有 24 个连续 NAN 或更多时才填充 NAN 值

python - Airflow Branch Operator 和任务组无效的任务 ID

html - 试图在中间的左侧表格和右侧的文字上获取照片。

python - 我怎样才能刮掉pdf的几页?

python - pyopenms : DLL load failed: The specified procedure could not be found

python - Scipy:ipython 笔记本中的并行计算?

jquery - 水平滚动内容

python - 在 BeautifulSoup 中查找标签和文本