【问题标题】:Extract links within a navigation div with python scrapy selectors使用 python scrapy 选择器提取导航 div 中的链接
【发布时间】:2017-09-20 20:45:12
【问题描述】:

我正在尝试提取 this webpage 中导航 div filterCategoryLevelOne 中的所有链接。

我用 Beautifulsoup 成功地做到了这一点,请求如下:

>> import requests
>> from bs4 import BeautifulSoup
>> url='http://uk.farnell.com/c/electrical'
>> response = requests.get(url)
>> soup = BeautifulSoup(source.content, "html.parser")
>> mydiv = soup.findAll("nav", { "class" : "filterCategoryLevelOne" })

my_div 为我列出了所有内容,然后我可以从中提取 href 值。

但是,如果我对一个scrapy shell 执行相同的操作,如下所示,我会得到其他东西:

>> scrapy shell -s USER_AGENT='Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9' http://uk.farnell.com/c/electrical
>> response.xpath('//nav[@class="filterCategoryLevelOne"]')
[<Selector xpath='//nav[@class="filterCategoryLevelOne"]' data='<nav class="filterCategoryLevelOne" aria'>]

如何在 scrapy 中进一步找到该 div 中的 href 值?

【问题讨论】:

  • 你试过extract()extract_first()

标签: python beautifulsoup scrapy


【解决方案1】:

您可以通过以下方式直接获取所有网址:

  response.xpath('//nav[@class="filterCategoryLevelOne"]//a/@href').extract()

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2019-10-05
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多