我正在尝试使用scrapy来抓取urls列表,并仅将找到特定关键字的urls作为输出。我通过shell尝试了if语句,它似乎起作用了--我尝试了if(..):exit(),它确实退出了shell--但是下面的输出是一个空文件(使用的url与在shell中返回if语句的url相同)。
def parse(self, response):
filename = f'file.txt'
if (response.css('*').re('keyword')):
with open(filename, 'wb') as f:
f.write(response.url)
self.log(f'Saved file {filename}')
发布于 2021-02-09 13:13:58
您以写二进制模式打开文件并尝试向其中写入字符串,这应该会引发错误。
而不是以附加模式打开文件:with open(filename, 'a') as f:
或者仅仅处于写入模式:with open(filename, 'a') as f:
https://stackoverflow.com/questions/66119288
复制