Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scraper收集了一些标题,而忽略了其余的_Python_Python 3.x_Xpath_Web Scraping - Fatal编程技术网

Python Scraper收集了一些标题,而忽略了其余的

Python Scraper收集了一些标题,而忽略了其余的,python,python-3.x,xpath,web-scraping,Python,Python 3.x,Xpath,Web Scraping,我已经用python编写了一个小脚本,使用xpath从yahoo finance的左侧区域中获取标题。该页面中有几个标题,但当我运行脚本时,我只得到三个标题。我不希望在这种情况下使用硒,因为我至少得到了最小的结果。我试着在脚本中使用“时间”来让网页完全加载。然而,似乎“时间”与此无关。除了使用selenium,我如何从该网页获取所有标题?以下是我尝试过的: import time import requests from lxml import html response = requests

我已经用python编写了一个小脚本,使用xpath从yahoo finance的左侧区域中获取标题。该页面中有几个标题,但当我运行脚本时,我只得到三个标题。我不希望在这种情况下使用硒,因为我至少得到了最小的结果。我试着在脚本中使用“时间”来让网页完全加载。然而,似乎“时间”与此无关。除了使用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)

我尝试了你的代码,显然我能得到的不仅仅是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

是否需要使用xpath选择器,或者是否可以使用BeautifulSoup?不需要。美丽的乌苏很好。