2016-06-22 12 views
1

div内にあるスパン内のURLを検索しようとしています。div内のスパン内のURLを検索するにはどうすればよいですか?

この場合、私が後にしているクラス "company_url"とのリンクです。

<div class="links standard"> 
 
    <span class="link"> 
 
     <a href="https://twitter.com/abacus" class="twitter_url icon_link fontello-twitter" rel="nofollow" target="_blank"></a> 
 
    </span> 
 
    <span class="link"> 
 
     <a href="http://www.facebook.com/abacuslabs" class="facebook_url icon_link fontello-facebook" rel="nofollow" target="_blank"></a> 
 
    </span> 
 
    <span class="link"> 
 
     <a href="https://www.linkedin.com/company/abacus-labs" class="linkedin_url icon_link fontello-linkedin" rel="nofollow" target="_blank"></a> 
 
    </span> 
 
    <span class="link"> 
 
     <a href="http://blog.abacus.com/" class="blog_url icon_link fontello-rss" rel="nofollow" target="_blank"></a> 
 
    </span> 
 
    <span class="link"> 
 
     <a href="http://abacus.com" class="company_url" rel="nofollow" target="_blank">abacus.com</a> 
 
    </span> 
 
</div>

は、私は、ページ内のdivとのdiv内のリンクを見つけるために私のXPathをテストしてみました。だから私は彼らが正しいと確信しています(私はhttp://www.freeformatter.com/xpath-tester.html#ad-outputを使用しました)。

しかし、私がコードを実行すると、何も取得されません。私は間違って何をしていますか?

from scrapy import Spider 
from scrapy.selector import Selector 
import datetime 
from saas.items import StartupItem 


class StackSpider(Spider): 
name = "abacus" 
allowed_domains = ["angel.co"] 
start_urls = [ 
    "https://angel.co/abacus", 
] 

def parse(self, response): 
    questions = Selector(response).xpath('//div[contains(@class, "links standard")]') 

    for question in questions: 
     item = StartupItem() 
     item['startupurl'] = question.xpath('/span[@class="link"]/a[@class="company_url"]/@href').extract()[0] 
     item['source'] = 'angel.co' 
     item['datetime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
     yield item 

答えて

1

次の2つの//を使用する必要があります。

あなたが一度あなたがあなたのURLを取得します:

In [2]: from lxml import html 

In [3]: x = html.fromstring(h) 

In [4]: d = x.xpath('//div[@class="links standard"]')[0] 

In [5]: d 
Out[5]: <Element div at 0x7f13c0a00208> 

In [6]: d.xpath('/span[@class="link"]/a[@class="company_url"]/@href') 
Out[6]: [] 

In [7]: d.xpath('.//span[@class="link"]/a[@class="company_url"]/@href') 
Out[7]: ['http://abacus.com'] 

正しいXPathのですが、ユーザーエージェントを追加する必要があることあなたがスキンシェルでview(response)を実行すると、あなたは次を参照してください:

~$ scrapy shell -s USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36" https://angel.co/abacus 

をそして、上記のコードを実行している:

ユーザーエージェントを追加

In [7]: d = response.xpath('//div[@class="links standard"]')[0] 

In [8]: d.xpath('/span[@class="link"]/a[@class="company_url"]/@href').extract_first() 

In [9]: d.xpath('.//span[@class="link"]/a[@class="company_url"]/@href').extract_first() 
Out[9]: u'http://abacus.com' 
+0

これらのXPathは動作しますが、Scrapyはまだデータをダウンロードしてくださいdoesntの。いくつかのグーグルを行った後(そして、別のサイトでそれをテストしてScrapyコードが良いことを証明した後)、私はAngel.coがボットに擦り傷をつけることを許さないという結論に向かっています。あなたの助けをありがとう! – user1287245

+0

私のノートブックに戻るときに私はサイトを見ます –

+0

@ user1287245、編集を参照してください。 settings.pyにユーザエージェントを追加する必要があります –

関連する問題