【问题标题】:WebScraping with BeautifulSoup or LXML.HTML使用 BeautifulSoup 或 LXML.HTML 进行网页抓取
【发布时间】:2011-03-30 23:03:42
【问题描述】:

我看过一些网络广播,在尝试这样做时需要帮助: 我一直在使用 lxml.html。雅虎最近改变了网络结构。

目标页面;

http://finance.yahoo.com/quote/IBM/options?date=1469750400&straddle=true

在 Chrome 中使用检查器:我在

中看到数据
 //*[@id="main-0-Quote-Proxy"]/section/section/div[2]/section/section/table

还有一些代码

如何将这些数据放入列表中。 我想将其他股票从“LLY”更改为“Msft”?
我如何在日期之间切换......并获得所有月份。

【问题讨论】:

标签: python web-scraping beautifulsoup lxml yahoo


【解决方案1】:

我知道你说过你不能使用lxml.html。但这里是如何使用该库来做到这一点,因为它是非常好的库。因此,为了完整起见,我提供了使用它的代码,因为我不再使用 BeautifulSoup ——它无人维护、速度慢且 API 丑陋。

以下代码解析页面并将结果写入 csv 文件。

import lxml.html
import csv

doc = lxml.html.parse('http://finance.yahoo.com/q/os?s=lly&m=2011-04-15')
# find the first table contaning any tr with a td with class yfnc_tabledata1
table = doc.xpath("//table[tr/td[@class='yfnc_tabledata1']]")[0]

with open('results.csv', 'wb') as f:
    cf = csv.writer(f)
    # find all trs inside that table:
    for tr in table.xpath('./tr'):
        # add the text of all tds inside each tr to a list
        row = [td.text_content().strip() for td in tr.xpath('./td')]
        # write the list to the csv file:
        cf.writerow(row)

就是这样! lxml.html 就是这么简单又好听!!可惜不能用。

以下是生成的results.csv 文件中的一些行:

LLY110416C00017500,N/A,0.00,17.05,18.45,0,0,17.50,LLY110416P00017500,0.01,0.00,N/A,0.03,0,182
LLY110416C00020000,15.70,0.00,14.55,15.85,0,0,20.00,LLY110416P00020000,0.06,0.00,N/A,0.03,0,439
LLY110416C00022500,N/A,0.00,12.15,12.80,0,0,22.50,LLY110416P00022500,0.01,0.00,N/A,0.03,2,50

【讨论】:

【解决方案2】:

这是一个从股票表中提取所有数据的简单示例:

import urllib
import lxml.html
html = urllib.urlopen('http://finance.yahoo.com/q/op?s=lly&m=2014-11-15').read()
doc = lxml.html.fromstring(html)
# scrape figures from each stock table
for table in doc.xpath('//table[@class="details-table quote-table Fz-m"]'):
    rows = []
    for tr in table.xpath('./tbody/tr'):
        row = [td.text_content().strip() for td in tr.xpath('./td')]
        rows.append(row)
    print rows

然后要提取不同的股票和日期,您需要更改 URL。这是前一天的 Msft: http://finance.yahoo.com/q/op?s=msft&m=2014-11-14

【讨论】:

  • 网站又变了.. finance.yahoo.com/quote/IBM/… . xpath : //*[@id="main-0-Quote-Proxy"]/section/section/div[2]/section/section/table 你能看看这个并更新你的答案..谢谢.. ..
  • @hoju...我添加了一些信息来提问,。 TIA
【解决方案3】:

如果您想要原始 json,请尝试 MSN

http://www.msn.com/en-us/finance/stocks/optionsajax/126.1.UNH.NYS/

您还可以指定过期日期?date=11/14/2014

http://www.msn.com/en-us/finance/stocks/optionsajax/126.1.UNH.NYS/?date=11/14/2014

如果你更喜欢 Yahoo json

http://finance.yahoo.com/q/op?s=LLY

但你必须从html中提取它

import re

m = re.search('<script>.+({"applet_type":"td-applet-options-table".+);</script>', resp.content)

data = json.loads(m.group(1))
as_dicts = data['models']['applet_model']['data']['optionData']['_options'][0]['straddles']

过期了

data['models']['applet_model']['data']['optionData']['expirationDates']

将iso转换为unix时间戳为here

然后用unix时间戳重新请求其他过期

http://finance.yahoo.com/q/op?s=LLY&date=1414713600

【讨论】:

  • 这只有一个月,第一个。我正在寻找所有的到期日期。我需要找到一种方法来迭代不同的日期。前一个月给了该月内的所有到期。
  • 通过日期工作,您认为有办法获取所有 11 月的数据吗?为什么使用 straddle =true?
  • 看起来你可以做任何一个,虽然两者都没有给你所有的到期时间
  • 您的方法是否适用于下面列出的更多选项。但是没有表他们使用 Ul --il 的msn.com/en-us/money/stockdetails/options/fi-126.1.UNH.NYS
  • 您仍然需要指定各个到期日期,但您可以直接请求json msn.com/en-us/finance/stocks/optionsajax/126.1.UNH.NYS/?date=11/…
【解决方案4】:

基于@hoju 的答案:

import lxml.html
import calendar
from datetime import datetime

exDate  = "2014-11-22"
symbol  = "LLY"
dt      = datetime.strptime(exDate, '%Y-%m-%d')
ym      = calendar.timegm(dt.utctimetuple())

url     = 'http://finance.yahoo.com/q/op?s=%s&date=%s' % (symbol, ym,)
doc     = lxml.html.parse(url)
table   = doc.xpath('//table[@class="details-table quote-table Fz-m"]/tbody/tr')

rows    = []        
for tr in table:
     d = [td.text_content().strip().replace(',','') for td in tr.xpath('./td')]
     rows.append(d)

print rows 

【讨论】:

    猜你喜欢
    • 2018-08-02
    • 1970-01-01
    • 2020-10-04
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    相关资源
    最近更新 更多