我从抓取的数据构造csv类型数据文件时遇到问题。我已经设法从表中抓取数据,但是在编写数据时,我已经有好几天了。我正在使用物品,并尝试将其写入 Pandas 数据框。我正在使用物品清单。
import scrapy
from wiki.items import WikiItem
import pandas as pd
class Spider(scrapy.Spider):
name = "wiki"
start_urls = ['https://datatables.net/']
def parse(self, response):
items = {'Name':[], 'Position':[], 'Office':[], 'Age':[],
'Start_Date':[],'Salary':[]}
trs = response.xpath('//table[@id="example"]//tr')
name = WikiItem()
pos = WikiItem()
office = WikiItem()
age = WikiItem()
start_data = WikiItem()
salary = WikiItem()
name['name'] = trs.xpath('//td[1]//text()').extract()
pos['position'] = trs.xpath('//td[2]//text()').extract()
office['office'] = trs.xpath('//td[3]//text()').extract()
age['age'] = trs.xpath('//td[4]//text()').extract()
start_data['start_data'] = trs.xpath('//td[5]//text()').extract()
salary['salary'] = trs.xpath('td[6]//text()').extract()
items['Name'].append(name)
items['Position'].append(pos)
items['Office'].append(office)
items['Age'].append(age)
items['Start_Date'].append(start_data)
items['Salary'].append(salary)
x = pd.DataFrame(items, columns=['Name','Position','Office','Age',
'Start_Date','Salary'])
yield x.to_csv("r",sep=",")
从这段代码中我得到的是这样;
,Name,Position,Office,Age,Start_Date,Salary
0,"{'name': [u'Tiger Nixon',
u'Garrett Winters',
u'Ashton Cox',
u'Cedric Kelly',
u'Airi Satou',
u'Brielle Williamson',
u'Herrod Chandler',
我得到了name列,但得到了59次,例如第一行是'Tiger Nixon'59次。我也得到59次排名栏,依此类推。抓取的数据也不是很好。我是新手,乐于接受任何帮助或建议。提前致谢!
编辑:我的items.py就是这样;
import scrapy
class WikiItem(scrapy.Item):
name = scrapy.Field()
position = scrapy.Field()
office = scrapy.Field()
age = scrapy.Field()
start_data = scrapy.Field()
salary = scrapy.Field()
参考方案
好的,因为我没有WikiItem的定义,所以我无法评论,也无法测试您的代码。但是让这个迭代遍历,好吗?
您可以检查一下这段代码得到什么吗?
class Spider(scrapy.Spider):
name = "wiki"
start_urls = ['https://datatables.net/']
def parse(self, response):
trs = response.xpath('//table[@id="example"]//tr')
if trs:
items = []
for tr in trs:
print tr.xpath('td[2]//text()').extract()
item = {
"Name": tr.xpath('td[1]//text()').extract(),
"Position": tr.xpath('td[2]//text()').extract(),
"Office": tr.xpath('td[3]//text()').extract(),
"Age": tr.xpath('td[4]//text()').extract(),
"Start_Date": tr.xpath('td[5]//text()').extract(),
"Salary": tr.xpath('td[6]//text()').extract()
}
items.append(item)
x = pd.DataFrame(items, columns=['Name','Position','Office','Age',
'Start_Date','Salary'])
yield x.to_csv("r",sep=",")
python JSON对象必须是str,bytes或bytearray,而不是'dict - python在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
无法注释掉涉及多行字符串的代码 - python基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…