当前位置: 首页 > 面试题库 >

如何一次加载无限滚动中的所有条目以解析python中的HTML

令狐俊风
2023-03-14
问题内容

我正在尝试从此页面提取信息。该页面一次加载10个项目,我需要滚动以加载所有条目(共100个)。我能够解析HTML并获取前10个条目所需的信息,但是我想在解析HTML之前完全加载所有条目。

我正在使用python,requests和BeautifulSoup。当页面加载前10个条目时,我解析页面的方式如下:

from bs4 import BeautifulSoup
import requests
s = requests.Session()
r = s.get('https://medium.com/top-100/december-2013')
page = BeautifulSoup(r.text)

但这只会加载前10个条目。因此,我查看了该页面,并获得了用于加载后续条目的AJAX请求,并且得到了响应,但它位于一个时髦的JSON中,我宁愿使用HTML解析器而不是解析JSON。这是代码:

from bs4 import BeautifulSoup
import requests
import json
s = requests.Session()
url = 'https://medium.com/top-100/december-2013/load-more'
payload = {"count":100}
r = s.post(url, data=payload)
page = json.loads(r.text[16:]) #skip some chars that throw json off

这给了我数据,但是它是一个很长且复杂的JSON,我宁愿将所有数据加载到页面上并简单地解析HTML。此外,呈现的HTML比JSON响应提供更多的信息(即作者的姓名,而不是晦涩的userID等)。这里存在类似的问题,但没有相关的答案。理想情况下,我想进行POST调用, 然后 请求HTML并对其进行解析,但是我无法做到这一点。


问题答案:

您将无法处理请求和BeautifulSoup,因为当您向下滚动时,要从中提取信息的页面将通过JS加载其余条目。您可以使用selenium来执行此操作,它会打开一个真正的浏览器,并且可以以编程方式传递向下翻页按键事件。观看此视频以查看操作。

http://www.tidbitsofprogramming.com/2014/02/crawling-website-that-loads-
content.html

以下是使用硒提取所有100个帖子标题的脚本

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()

browser.get("https://medium.com/top-100/december-2013")
time.sleep(1)

elem = browser.find_element_by_tag_name("body")

no_of_pagedowns = 20

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1

post_elems = browser.find_elements_by_class_name("post-item-title")

for post in post_elems:
    print post.text

输出:

When Your Mother Says She’s Fat
When “Life Hacking” Is Really White Privilege
As tendências culturais dos anos 2000 adiantadas pelo É o Tchan na década de 90
Coming Out as Biracial
Como ganhar discussões com seus parentes de direita neste Natal
How to save local bookstores in two easy steps
Welcome to Dinovember
How to Piss Off Your Barista
The boy whose brain could unlock autism
CrossFit’s Dirty Little Secret
Welcome to Medium
Here’s How the Military Wasted Your Money in 2013
Why I Wear Nail Polish
The day of High School I’ll never forget
7 Reasons Buffalonians Shouldn’t Hate Snow
Dear Guy Who Just Made My Burrito:
Is the Mona Lisa Priceless?
Please stop live tweeting people’s private conversations
Your Friends and Rapists
Eight things you can live without
The Value of Content
40 Ways To Make Life Simple Again
Manila-Beijing-Washington:
Things I Wish Someone Had Told Me When I Was Learning How to Code
Dear Ticketmaster,
Steve Jobs Danced To My Song
11 Things I Wish I Knew When I Started My Business
Bullish: Benevolent Sexism and “That Guy” Who Makes Everything Awkward
Advice to a College Music Student
Silver Gyninen joutui sotaan
Imagining the Post-Antibiotics Future
Which side are you on?
Put it away, junior. 
Casual Predation
The sad little iPhone commercial
How Node.js is Going to Replace JavaScript
Why you should have your heart broken into a million little pieces. 
How to Write Emails Like a CEO
Designing Products That Scale
How radioactive poison became the assassin’s weapon of choice
Why do people hate CrossFit?
We (Still) Need Feminism
10 Advanced Hearthstone Arena Tips
Let It Full-Bleed
What Medium Is For
How a Small Force of Finnish Ski Troops Fought Off a Massive Soviet Army
An Introvert’s Guide to Better Presentations
Mandela The Terrorist
Why You Should have a Messy Desk
Why I’m Not a TEDx Speaker
Fonts have feelings too
You Don’t Want Your Thanksgiving to Go Like This
What I’ve Learned in My First Month as a VC
Why Quantity Should be Your Priority
My Airbnb story
I Wanna Date You Like An Animal
The GIF Guide to Getting Paid
How We Discovered the Underground Chinese App Market
First Images of a Heart Injected with Liquid Metal 
Beyonce Broke the Music Business
“View mode” approach to responsive web design
Sometimes You Will Forget Your Mom Has Cancer
Darkness Ray Beams Invisibility From A Distance
Why Work As We Know It May Be Immoral
Staying Ahead of the Curve
The Geekiest Game Ever Made Has Been Released In Germany 
The Dirty Secret Behind the Salesforce $1M Hackathon
I’m a really good impostor
Mathematical Model of Zombie Epidemics Reveals Two Types of Living-Dead Infections
The Heartbreak Kid
200 Things
I’m Not Racist But—
Duel of the Superbattleships
23 and You
The Seattle NO
I’m a vaccine refuser. There, I said it. 
The Year We Broke Everything
How to make a DIY home alarm system with a raspberry pi and a webcam
Strike While the App is Hot
How to Fall In (and Out) of Love:
Why did Google make an ad for promoting “Search” in India where it has over 97% market share?
A Holiday Message From Jesus
Revealed: The Soviet Union’s $1 Billion ‘Psychotronic’ Arms Race with the US
Postmortem of a Venture-backed Startup
The 1.x Crore Myth
The “Getting Shit Done” Sleep Cycle 
Is the F-35 Joint Strike Fighter the New F-4?
Can the F-35 Win a Dogfight?
Responsive Photosets
Fightball: Millennials vs Boomers
The iconicity of “peaceful resistance”
How We Make Chocolate
Five Ships of the Chinese Navy You Really Ought to Know About
Glassholes and Black Rock City
Bad News for U.S. Warplane Pilots: Russia’s New Dogfighting Missile Can’t Miss
How Antisec Died
10 ways you’ll probably f**k up your startup
UPDATED: Finding the unjustly homeless, and teaching them to code.
Technology hasn’t Changed Us.
What I’ve learned from fatherhood


 类似资料:
  • 我在节点中运行Express。js服务器,作为我的React前端应用程序的备份。 前端应用程序通过REST调用从后端(存储在Mongo中)获取数据,并在表中显示这些数据。数据量每天都在增长,所以我想我应该考虑减少传输到前端应用程序的数据量,以避免后端不必要的压力。 我不确定这是否是正确的方法,但我一直在考虑让backen获取有限的条目,以便只有这些数据才会显示在前端表中。 搜索时会出现问题-当用户

  • 问题内容: 我有一个页面(我们称其为1.php),它使用jQuery-ajax将2.php加载到div- box中。2.php从我的数据库中打印20条记录。当滚动时到达div框的底部时,我希望它加载接下来的20条记录。像Facebook,Twitter等,都是这样做的。 现在,我已经有了这种行为,但是仅当自己加载2.php时!但不在div框中。 我该怎么办? 提前致谢! 问题答案: 文件1.php

  • 问题内容: 我正在尝试编写一个Python脚本,该脚本将定期检查网站以查看某项是否可用。过去,我已经成功使用了request.get,lxml.html和xpath来自动执行网站搜索。对于此特定URL(http://www.anthropologie.com/anthro/product/4120200892474.jsp?cm_vc=SEARCH_RESULTS#/)和同一网站上的其他URL ,

  • 我无法让x和y滚动条在使用Python的Tkinter中工作,尽管我已经遵循了多个示例: 如何在python 3.4中使用tkinter添加2个滚动条 在tkinter中添加滚动条到一组小部件 如何在tkinter中制作一个合适的双滚动条框架 垂直和水平滚动条上的Tkinter小部件 水平和垂直滚动画布小部件 滚动条会出现,但当窗口小于框架时不会激活。我怎样才能让它工作(见下图)? 下面是产生我的

  • 问题内容: 我想要一个jQuery进度栏,该进度栏会根据服务器端请求的状态进行更新。我基于本教程编写此代码,但它使用文件上传器作为基础(与此问题相同)。没有文件上传器,我无法使其工作完全相同。问题是进度条仅在process.php完成后才更新。它等待整个过程完成,而不是异步请求进度更新。我只看到数据:数据警报一次。 有任何想法吗? 网页: Process.php-提交表单时调用 javascrip

  • 问题内容: 我使用Python来解析日志文件中的条目,并使用Tkinter显示条目内容,到目前为止这是非常棒的。输出是一个标签小部件的网格,但有时有更多的行无法显示在屏幕上。我想添加一个滚动条,看起来应该很简单,但我想不通。 文档意味着只有列表、文本框、画布和条目小部件支持滚动条界面。这些似乎都不适合显示小部件网格。可以在画布小部件中放置任意小部件,但你似乎必须使用绝对坐标,因此我将无法使用网格布