我正在尝试解析网络表格并将某些数据导出到 csv 文件中。
我不知道形成两个 XPath 后跟一个 for 语句(或者两个是正确的?)。
当前蜘蛛:
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
关于Python:如何将两个 for 语句变成一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29872462/