python - Scrapy - href 链接中的简单文本刮刀
问题描述
我现在是 2 年的新手,所以这可能是多余的问题。我正在尝试抓取一个简单的文本,它是 href 链接中的一个故事。我尝试了 //a.text () 使用scrapy shell,但它没有刮掉任何东西。我还想输出到数据库或文本文件(scrapy 项目)。该网站是: http ://www.nderf.org/Experiences/
编辑: 现在,我可以抓取链接的标题,但不能抓取@href 标记中的文本(故事)。
import scrapy
from ..items import NdeItem
class NDESpider(scrapy.Spider):
name='NDEstories'
allowed_domains=["nderf.org"]
headers=['author','desc','story']
download_delay=1.5
item = []
start_urls = ["http://www.nderf.org/Archives/exceptional.html"]
def start_requests(self):
requests = []
for item in self.start_urls:
requests.append(scrapy.Request(url=item, headers={'Referer': 'https://www.google.com/'}))
return requests
def parse(self, response):
#/ html / body / div[2] / section[2] / div / p / strong / span / a
a_selectors=response.xpath('//a[@class="MiniLink"]')
href_selector=response.xpath('@href')
for selector in a_selectors:
items = []
item = NdeItem() # type: NdeItem
item['author'] = selector.xpath('text()').extract_first()
item['desc'] = selector.xpath('@href').get()
item['story'] = selector.xpath('@href/text()').get()
items.append(item)
# Create a new Request object
request = response.follow(item['desc'], callback=self.parse)
# Return it thanks to a generator
with open ('log.txt','a') as f:
f.write('author:{0}\n desc:{1}\n story:{2}\n'.format(item['author'],item['desc'], item['story']))
yield request
解决方案
response.xpath('.//a/@href').get()
推荐阅读
- java - 在 Java 中使用空格编码 URL 工作不正确
- javascript - 使用 jquery 将包装器添加到 div 的最佳方法是什么?
- python - python请求点击数据后获取
- javascript - 通过 React Navigation Drawer 的道具
- c# - C# LINQ 合并 2 个列表的值
- java - Spark Java:无法更改驱动程序内存
- ios - 日期在 iOS swift 中返回 nil
- moryx - 如何执行自定义 ResourceInitializer
- cucumber - 魅力报告:如何在魅力报告中隐藏敏感数据?
- android - 滑动刷新recyclerview重复项json