Python 使用BeautifulSoup或LXML.HTML进行Web垃圾处理

Python 使用BeautifulSoup或LXML.HTML进行Web垃圾处理,python,web-scraping,beautifulsoup,lxml,yahoo,Python,Web Scraping,Beautifulsoup,Lxml,Yahoo,我看过一些网络广播,需要帮助才能做到这一点: 我一直在使用lxml.html。雅虎最近改变了网络结构 目标页面 在Chrome中使用inspector:我在 //*[@id="main-0-Quote-Proxy"]/section/section/div[2]/section/section/table 然后是一些代码 如何将这些数据输出到列表中。 我想把其他股票从“LLY”改为“Msft”? 如何在日期之间切换…并获得所有月份 我知道你说过你不能用。但这里是如何使用该库来实现它的,因为

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

目标页面

在Chrome中使用inspector:我在

 //*[@id="main-0-Quote-Proxy"]/section/section/div[2]/section/section/table
然后是一些代码

如何将这些数据输出到列表中。 我想把其他股票从“LLY”改为“Msft”?

如何在日期之间切换…并获得所有月份

我知道你说过你不能用。但这里是如何使用该库来实现它的,因为它是一个非常好的库。因此,为了完整起见,我提供了使用它的代码,因为我不再使用
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

下面是一个从库存表中提取所有数据的简单示例:

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:
如果您想要原始json,请尝试MSN

http://www.msn.com/en-us/finance/stocks/optionsajax/126.1.UNH.NYS/
您还可以指定到期日期
?日期=2014年11月14日

http://www.msn.com/en-us/finance/stocks/optionsajax/126.1.UNH.NYS/?date=11/14/2014
如果你喜欢雅虎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']
将iso转换为unix时间戳

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

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

基于@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 

到目前为止你试过做什么?以下是BeautifulSoup的快速入门指南:+1感谢您的示例,我以前只使用过BeautifulSoup,甚至没有意识到它的@user428862:我已经在代码中添加了注释,解释了它是如何工作的。上面对其他库的链接建议已经过时了,而且似乎是由于放弃了BeautifulSoup 3。Beautifulsoup4有了更多的最新更新,在这里的一个活跃论坛只有一个月,第一个月。我想知道所有的有效期。我需要找到一种迭代不同日期的方法。在一个月之前给出了该月内的所有到期日。通过计算日期,你认为有没有办法获得所有11月数据。为什么要使用straddle=true?看起来您可以选择其中一种,尽管这两种方法都没有给出所有的过期时间。下面列出了您的方法的更多选项。但是他们没有使用Ul--il的表格,你仍然需要指定个别的到期日期,但是你可以直接请求json再次更改站点。xpath://*[@id=“main-0-Quote-Proxy”]/section/section/div[2]/section/section/table请您查看并更新您的答案。。谢谢…@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