麻烦让美丽的汤回归电影片名&来自h3类的分数在An href中

时间:2017-01-24 03:52:28

标签: python web web-scraping web-crawler

这是网站......

https://www.rottentomatoes.com/browse/dvd-top-rentals/?services=amazon;amazon_prime;fandango_now;hbo_go;itunes;netflix_iw;vudu

右键单击检查页面中间海报下面的标题会显示代码。我从教程到发布过程中尝试了太多变化。 这就是我的Python脚本......

import requests
from bs4 import BeautifulSoup

url = "https://www.rottentomatoes.com/browse/dvd-top-rentals/?services=amazon;amazon_prime;fandango_now;hbo_go;itunes;netflix_iw;vudu"

r = requests.get(url)

soup = BeautifulSoup(r.content, "lxml")

下一步????????

1 个答案:

答案 0 :(得分:2)

此页面由JavaScript呈现,requests仅返回html代码: enter image description here

真实数据在此网址中:

https://www.rottentomatoes.com/api/private/v2.0/browse?page=1&limit=30&type=dvd-top-rentals&services=amazon%3Bamazon_prime%3Bfandango_now%3Bhbo_go%3Bitunes%3Bnetflix_iw%3Bvudu&sortBy=popularity

代码:

import requests

r = requests.get('https://www.rottentomatoes.com/api/private/v2.0/browse?page=1&limit=30&type=dvd-top-rentals&services=amazon%3Bamazon_prime%3Bfandango_now%3Bhbo_go%3Bitunes%3Bnetflix_iw%3Bvudu&sortBy=popularity')

data = r.json()
for result in data["results"]:
    print(result["title"], result["tomatoScore"])

出:

The Girl on the Train 43
Keeping Up With The Joneses 19
Ouija: Origin of Evil 82
Long Way North (Tout en haut du monde) 98
The Whole Truth 29
Come And Find Me 67
LEGO Jurassic World: The Indominus Escape None
My Father, Die 88
When Elephants Were Young None
Roger Corman's Death Race 2050 None
Take the 10 None
Deepwater Horizon 83
The Accountant 51
The Birth of a Nation 72
Kevin Hart: What Now? 76

答案:

  1. 如何知道网站是否由JavaScript呈现?
  2. 当您需要抓取网站时,只需在浏览器中禁用JavaScript,检查页面内容是否已更改。 enter image description here

    我在chrome中使用此扩展程序只需单击一下即可禁用JS。

    1. 如何在浏览器中找到真实的网址?
    2. enter image description here 使用chrome dev-tools的网络来监控网络活动,即使页面使用JS来获取数据,它仍然需要向服务器发出请求,你可以在网络选项卡中找到这些请求。

      1. u''是python2中unicode的表示,它是python3中的默认设置。它只在python中显示,不需要担心它。