我是 Scrapy 的新手。我正在尝试从“https://www.tysonprop.co.za/agents/”中抓取内容以用于工作目的。
特别是,我正在寻找的信息似乎是由脚本标签生成的。
行:<%= branch.branch_name %> 在运行时解析为:Tyson Properties Head Office。
我正在尝试访问在运行时在 h2 元素内生成的文本。
但是,Scrapy 响应对象似乎抓取了原始源代码。即我想要的数据显示为 <%= branch.branch_name %> 而不是“Tyson Properties Head Office”。
任何帮助,将不胜感激。
HTML 响应对象提取:
<script type="text/html" id="id_branch_template">
<div id="branch-<%= branch.id %>" class="clearfix margin-top30 branch-container" style="display: none;">
<h2 class="grid_12 branch-name margin-bottom20"><%= branch.branch_name %></h2>
<div class="branch-agents container_12 first last clearfix">
<div id="agents-list-left" class="agents-list left grid_6">
</div>
<div id="agents-list-right" class="agents-list right grid_6">
</div>
</div>
</div>
</script>
<script type="text/javascript">
当前 Scrapy 蜘蛛代码:
import scrapy
from scrapy.crawler import CrawlerProcess
class TysonSpider(scrapy.Spider):
name = 'tyson_spider'
def start_requests(self):
url = 'https://www.tysonprop.co.za/agents/'
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
script = response.xpath('//script[@id="id_branch_template"]/text()').get()
div = scrapy.Selector(text=script).xpath('//div[contains(@class,"branch-container")]')
h2 = div.xpath('./h2[contains(@class,"branch-name")]')
与此问题相关: Scrapy xpath not extracting div contains special characters <%=