I've attached a picture.
The problem I'm facing is that getting the first element of same class. I'm trying to get
.adxHeader
> .adxExtraInfo (1st one)
> .adxExtraInfoPart (1st one)
> a::text
I wrote the following code but not working. Any Idea?
response.css('div.adxViewContainer div.adxHeader div.adxExtraInfo:nth-child(1) div.adxExtraInfoPart:nth-child(1) a::text').extract_first()
expected output: الرياض
<div class="adxHeader">
<h3 itemprop="name"> » درج داخلي للاجار جديد حي المونسيه</h3>
<div class="adxExtraInfo">
<div class="adxExtraInfoPart"><a href="/city/الرياض"><i class="fa fa-map-marker"></i> الرياض</a></div>
<div class="adxExtraInfoPart"><a href="/users/ابو نوره"><i class="fa fa-user"></i> ابو نوره</a></div>
</div>
<div class="adxExtraInfo">
<div class="adxExtraInfoPart"> قبل ساعه و 27 دقيقه</div>
<div class="adxExtraInfoPart">#20467014</div>
</div>
<div class="moveLeft">
<a href="www.google.com" class="nextad"> ← التالي </a>
<br />
</div>
</div>
You could use xpath instead of css:
response.xpath('(//div[@class="adxExtraInfo"])[1]//a/text()').extract_first()
The <div class="adxExtraInfo">
that you are targetting is not the 1st child of its <div class="adxHeader">
parent. The <h3>
is.
So div.adxExtraInfo:nth-child(1)
will not match anything in your input:
>>> s = scrapy.Selector(text='''<div class="adxHeader">
... <h3 itemprop="name"> » درج داخلي للاجار جديد حي المونسيه</h3>
...
... <div class="adxExtraInfo">
... <div class="adxExtraInfoPart"><a href="/city/الرياض"><i class="fa fa-map-marker"></i> الرياض</a></div>
... <div class="adxExtraInfoPart"><a href="/users/ابو نوره"><i class="fa fa-user"></i> ابو نوره</a></div>
... </div>
...
... <div class="adxExtraInfo">
... <div class="adxExtraInfoPart"> قبل ساعه و 27 دقيقه</div>
... <div class="adxExtraInfoPart">#20467014</div>
... </div>
... <div class="moveLeft">
...
...
... <a href="www.google.com" class="nextad"> ← التالي </a>
... <br />
...
... </div>
...
... </div>''')
>>> s.css('div.adxHeader > div.adxExtraInfo:nth-child(1)').extract()
[]
>>> s.css('div.adxHeader > *:nth-child(1)').extract()
[u'<h3 itemprop="name"> \xbb \u062f\u0631\u062c \u062f\u0627\u062e\u0644\u064a \u0644\u0644\u0627\u062c\u0627\u0631 \u062c\u062f\u064a\u062f \u062d\u064a \u0627\u0644\u0645\u0648\u0646\u0633\u064a\u0647</h3>']
>>>
But you may want to anchor div.adxExtraInfo
with the <h3>
in that case, using the Adjacent sibling combinator (in other words, the <div class="adxExtraInfo">
immediately following the <h3>
):
>>> print(
... s.css('''div.adxHeader
... > h3:nth-child(1) + div.adxExtraInfo
... div.adxExtraInfoPart:nth-child(1) a::text''').extract_first())
الرياض
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With