簡體   English   中英

Python:如何將兩個for語句合二為一?

[英]Python: How to turn two for-statements into one?

我正在嘗試解析Web表並將某些數據導出到csv文件中。

我不知道要形成兩個XPath,然后是一個for-statement(或者兩個是正確的?)。

當前蜘蛛:

class MySpider(BaseSpider):
    symbols = ["SCMP"]
    name =  "dozen"
    allowed_domains = ["yahoo.com"]     
    start_urls = ["http://finance.yahoo.com/q/is?s=SCMP&annual"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        revenue = response.xpath('//td[@align="right"]/strong/text()')
        date = response.xpath('//tr[@class="yfnc_modtitle1"]/th/text()')
        items = []
        for rev in revenue:
            item = DozenItem()
            item["Revenue"] = rev.re('\d*,\d*')
            items.append(item)
        return items[:3]
        days = []
        for day in dates:
            item = DozenItem()
            item["Date"] = day.re('\d*')
            days.append(item)
        return items[:3]

我知道這需要工作,我只是不確定要走哪個方向......?

這是輸出:

出口

可見,我無法填寫日期。

這是我正在解析日期的html:

<TR class="yfnc_modtitle1" style="border-top:none;">

<th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2014</th>

<th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2013</th>
<th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2012</th>
</TR>
<tr>
<td colspan="2">
for rev, day in zip(revenue, dates):
    pass # code here
class MySpider(BaseSpider):
    symbols = ["SCMP"]
    name =  "dozen"
    allowed_domains = ["yahoo.com"]     
    start_urls = ["http://finance.yahoo.com/q/is?s=SCMP&annual"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        revenue = response.xpath('//td[@align="right"]/strong/text()')
        date = response.xpath('//tr[@class="yfnc_modtitle1"]/th/text()')
        items = []
        for rev, day in zip(revenue, dates):
            item = DozenItem()
            item["Revenue"] = rev.re('\d*,\d*')
            item["Date"] = day.re('\d*')
            items.append(item)
        return items[:3]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM