【发布时间】:2014-02-06 02:12:48
【问题描述】:
我正在尝试从包含各种 HTML 元素和一系列嵌套表格的页面中抓取项目。
我有一些代码正在工作,成功地从表 X 中抓取 class="ClassA" 并将表元素输出到一系列项目中,例如公司地址、电话号码、网站地址等。
我想在我输出的这个列表中添加一些额外的项目,但是要抓取的其他项目不在同一个表中,有些甚至根本不在表中,例如 页面另一部分的标签。
如何使用 xpath 过滤器将其他一些项目添加到我的输出中并让它们出现在相同的数组/输出结构中?我注意到,如果我从另一个表中刮取额外的表项(即使该表具有完全相同的 CLASS 名称和 ID),这些其他项的 CSV 输出也会在 CSV 中的不同行上输出,而不是保持 CSV 结构完整:(
我确定必须有一种方法可以让项目在 csv 输出中保持统一,即使它们是从页面上略有不同的区域抓取的?希望它只是一个简单的修复...
----- HTML 示例页面正在被抓取 -----
<html>
<head></head>
<body>
< // huge amount of other HTML and tables NOT to be scraped >
<h2>HEADING TO BE SCRAPED - Company Name</h2>
<p>Company Description</p>
< table cellspacing="0" class="contenttable company-details">
<tr>
<th>Item Code</th>
<td>IT123</td>
</tr>
<th>Listing Date</th>
<td>12 September, 2011</td>
</tr>
<tr>
<th>Internet Address</th>
<td class="altrow"><a href="http://www.website.com/" target="_top">http://www.website.com/</a></td>
</tr>
<tr>
<th>Office Address</th>
<td>123 Example Street</td>
</tr>
<tr>
<th>Office Telephone</th>
<td>(01) 1234 5678</td>
</tr>
</table>
<table cellspacing="0" class="contenttable" id="staff">
<tr><th>Management Names</th></tr>
<tr>
<td>
Mr John Citizen (CEO)<br/>Mrs Mary Doe (Director)<br/>Dr J. Watson (Manager)<br/>
</td>
</tr>
</table>
<table cellspacing="0" class="contenttable company-details">
<tr>
<th>Contact Person</th>
<td>
Mr John Citizen<br/>
</td>
</tr>
<tr>
<th class=principal>Company Mission</th>
<td>ACME Corp is a retail sales company.</td>
</tr>
</table>
</body>
</html>
---- 抓取代码示例----
from scrapy.spider import Spider
from scrapy.selector import Selector
from my.items import AsxItem
class MySpider(Spider):
name = "my"
allowed_domains = ["website.com"]
start_urls = ["http://www.website.com/ABC" ]
def parse(self, response):
sel = Selector(response)
sites = sel.xpath('//table[@class="contenttable company-details"]')
items = []
for site in sites:
item = MyItem()
item['Company_name'] = site.xpath('.//h1//text()').extract()
item['Item_Code'] = site.xpath('.//th[text()="Item Code"]/following-sibling::td//text()').extract()
item['Listing_Date'] = site.xpath('.//th[text()="Listing Date"]/following-sibling::td//text()').extract()
item['Website_URL'] = site.xpath('.//th[text()="Internet Address"]/following-sibling::td//text()').extract()
item['Office_Address'] = site.xpath('.//th[text()="Office Address"]/following-sibling::td//text()').extract()
item['Office_Phone'] = site.xpath('.//th[text()="Office Telephone"]/following-sibling::td//text()').extract()
item['Company_Mission'] = site.xpath('//th[text()="Company Mission"]/following-sibling::td//text()').extract()
yield item
输出到 CSV
scrapy crawl my -o items.csv -t csv
使用上面的示例代码,[companymission] 项目出现在 CSV 中与其他项目不同的行上(猜测是因为它在不同的表中)即使它具有相同的 CLASS 名称和 ID,并且另外我不确定如何抓取
字段,因为它超出了我当前 XPATH 站点过滤器的表结构?
我可以扩展站点 XPATH 过滤器以包含更多内容,但这不会降低效率并破坏一起过滤的意义吗?
这是调试日志的示例,您可以在其中看到公司任务由于某种原因被处理了两次,并且第一个循环是空的,这一定是它输出到 CSV 中的新行的原因,但是为什么??
{'Item_Code': [u'ABC'],
'Listing_Date': [u'1 January, 2000'],
'Office_Address': [u'Level 1, Some Street, SYDNEY, NSW, AUSTRALIA, 2000'],
'Office_Fax': [u'(02) 1234 5678'],
'Office_Phone': [u'(02) 1234 5678'],
'Company_Mission': [],
'Website_URL': [u'http://www.company.com']}
2014-02-06 16:32:13+1000 [my] DEBUG: Scraped from <200 http://www.website.com/Code=ABC>
{'Item_Code': [],
'Listing_Date': [],
'Office_Address': [],
'Office_Fax': [],
'Office_Phone': [],
'Company_Mission': [u'The comapany is involved in retail, food and beverage, wholesale services.'],
'Website_URL': []}
我完全困惑的另一件事是为什么项目在 CSV 中以与 HTML 页面上的项目和我在蜘蛛配置文件中定义的顺序完全不同的顺序吐出。 scrapy 是否以它喜欢的任何顺序完全异步运行返回项目?
【问题讨论】:
标签: csv xpath web-scraping scrapy