[英]Python: How to turn two for-statements into one?
I'm trying to parse a web table and export certain data into a csv file. 我正在尝试解析Web表并将某些数据导出到csv文件中。
I'm ignorant to forming two XPaths followed by a single for-statement (or maybe two is correct?). 我不知道要形成两个XPath,然后是一个for-statement(或者两个是正确的?)。
Current Spider: 当前蜘蛛:
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]
I know this needs work, I'm just not sure which direction to go...? 我知道这需要工作,我只是不确定要走哪个方向......?
This is the output: 这是输出:
As is visible, I can't get the dates to fill in. 可见,我无法填写日期。
Here is the html I'm parsing the dates from: 这是我正在解析日期的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.