我正在使用scrapy从www.tf2items.com/profiles/抓取用户及其SteamID的列表。
目前,我的代码如下所示:
import scrapy
bot_words = [
"bot",
"BOT",
"[tf2mart]"
]
class AccountSpider(scrapy.Spider):
name = "accounts"
start_urls = [
'file:///Users/max/Documents/promotebot/tutorial/tutorial/TF2ITEMS.htm'
]
def parse(self, response):
for tr in response.css("tbody"):
user = response.css("span a").extract()
print(user)
if bot_words not in response.css("span a").extract():
for href in response.css("span a::attr(href)").extract():
#yield response.follow("http://www.backpack.tf" + href, self.parse_accounts)
print("this is a value")
我的最终目标是使此代码打印出类似以下内容的内容:
a href =“ / profiles / 76561198042757507”> Kchypark
这是一个值
a href =“ / profiles / 76561198049853548”> Agen Kolar
这是一个值
a href =“ / profiles / 76561198036381323”> Grave Shifter15
这是一个值
有了当前代码,我什至可以期待
a href =“ / profiles / 76561198042757507”> Kchypark
这是一个值
这是一个值
这是一个值
a href =“ / profiles / 76561198049853548”> Agen Kolar
这是一个值
这是一个值
这是一个值
a href =“ / profiles / 76561198036381323”> Grave Shifter15
这是一个值
这是一个值
这是一个值
但是,我得到:
a href =“ / profiles / 76561198042757507”> Kchypark
a href =“ / profiles / 76561198049853548”> Agen Kolar
a href =“ / profiles / 76561198036381323”> Grave Shifter15
这是一个值
这是一个值
这是一个值
我究竟做错了什么?
您的第一个输出输出href
s的列表
user = response.css("span a").extract()
print(user)
您的代码应该看起来像
def parse(self, response):
for tr in response.css("tbody"):
for user in response.css("span a"):
if bot_words not in user:
print(user.extract())
href = user.css('::attr(href)').extract()[0]
print(href)
#yield response.follow("http://www.backpack.tf" + href, self.parse_accounts)
print("this is a value")
同样,在处理方面的最佳实践是使用项目而不是原始print
功能。
并注意像 response.css("span a").extract()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句