我在python中使用xpath编写了一个小脚本,以便从雅虎财务中删除左侧区域显示的标题。该页面中有几个标题,但是当我运行我的脚本时,我只获得了三个标题。我不希望在这种情况下使用硒,因为我至少得到了最低限度的结果。我试图在我的脚本中使用“时间”让网页加载完整。然而,似乎“时间”与它无关。除了使用selenium之外,如何从该网页获取所有标题?这是我试过的:
import time
import requests
from lxml import html
response = requests.get("https://finance.yahoo.com/").text
time.sleep(5)
root = html.fromstring(response)
for title in root.xpath("//a[u[@class='StretchedBox']]/text()"):
print(title)
答案 0 :(得分:0)
我尝试了你的代码,显然我能获得的不仅仅是3个游戏。
雅虎可能只是改变了网站动态,使你的代码能够解析更多的标题。
输出:
This could get 'very ugly, very fast' for Boeing, says one market expert
Toys R Us submits plan to liquidate its US business, will close or sell all US stores
Sears Gains After Tax Benefit Helps Retailer Swing to a Profit
Blood, Fraud and Money Led to Theranos CEO's Fall From Grace
Why Cisco Systems Is Looking Like a Must-Own Stock
Realty Income (O) Announces Hike in Monthly Dividend Payment
Tillerson's exit has 'very big implications' for oil prices: RBC’s Helima Croft
Kevin O'Leary shares his No. 1 trick for tipping at a res...
For J C Penney Co. Inc. Stock Survival is the Victory
Why Ford (F) Stock Popped Despite Recalling 1.4 Million Vehicles
Toys 'R' Us Liquidation Reveals a Dangerous Trend Forming in U.S. Retailing
Theranos CEO Elizabeth Holmes settles with SEC, agrees to...
Manafort Urges Judge to Dismiss Laundering and Lobbying Case
‘Dow Theory’ Warning Signal Spells Trouble for These 5 Stocks
General Electric Company Stock Is Ugly, But It Has Potential for the Gambler
This popular Dow stock looks ready to join GE on the discard pile
Intel's Broadcom Buyout Rumors Put These 3 Stocks at Risk
The SEC isn't sending Theranos CEO Elizabeth Holmes to jail for fraud — here are her punishments
Former Equifax CIO Charged With Insider Trading in Data Breach
Lyft is teaming up with automotive industry giant Magna to develop self-driving cars — and getting a $200 million investment (MGA, GM, GOOGL)
Google, Apple face EU law on business practices
Disney Elevates Two Top Executives in Possible CEO Bake-Off
Nokia Oyj Stock Could Break out over the Next Two Years
Wall Street gains as White House plays down trade war chances
U.S. retail sales falter; inflation creeping higher