CSS 选择器应用于从网页中提取数据操作实践。
下面实践案例是从百度新闻网页中财经板块获取新闻网址来源,为了快速调试CSS提取表达式,下面使用 Scrapy shell来完成。
(py3env) MacBook ~/python/tutorial/MySpider$ scrapy shell http://news.baidu.com/finance
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x10a8ecda0>
[s] item {}
[s] request <GET http://news.baidu.com/finance>
[s] response <200 http://news.baidu.com/finance>
[s] settings <scrapy.settings.Settings object at 0x10b6f19b0>
[s] spider <baidunewsSpider 'baidunews' at 0x10c2211d0>
[s] Useful shortcuts:
[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s] fetch(req) Fetch a scrapy.Request and update local objects
[s] shelp() Shell help (print this help)
[s] view(response) View response in a browser
In [1]:response.css(".logo .logo a::attr(href)").extract()
Out[1]: ['http://news.baidu.com/']
In [2]: response.css(".ulist li a::text").extract()
Out[2]:
['受美股拖累大跌,A股或“见底”?',
'特朗普经历上台以来“最黑暗时刻”,美国股市持续暴跌',
'阿里巴巴股价自由落体,马云:美国终将“损失更多”!',
'美股大跌 A股也将血雨腥风?',
'美股暴跌的背后:特朗普错了还是“美联储疯了”?',
'宝马收购华晨宝马25%股权,股比增至75%,华晨或..',
'美国股市再度下挫 道指跌逾2%',
'美国三大股指再度集体下跌 道指跌逾2%',
'历史数据告诉你 沪指暴跌次日大概率这么走',
'特朗普该哭了!股市行情反映出美国发动的贸易战谁胜谁..',
'美股暴跌,A股也跟着大跌,该怎么办?',
'中国女首善陷280亿债务危机,曝欠薪两月,承诺捐款..',
'牛市早报|欧美股市暴跌遭遇“黑色星期三”,昂利康今..',
'美股暴跌 美媒嘲讽特朗普“这下牛皮吹爆了吧”',
'央行行长易纲:杠杆已稳 年初预定增长目标可以实现',
'离岸人民币盘中反弹近750点 连破四关口收复6.8..',
'美股历次暴跌 A股怎么走?',
'马云家族再成首富 前三家总身家7600亿元',
'2018胡润百富榜公布 19位“秦商”上榜',
'全球股市大溃败!欧洲市场接力惨跌',
'“全球股民今天抱在一起瑟瑟发抖”',
'受美股拖累大跌,A股或“见底”?',
'特朗普经历上台以来“最黑暗时刻”,美国股市持续暴跌',
'阿里巴巴股价自由落体,马云:美国终将“损失更多”!',
'美股大跌 A股也将血雨腥风?',
'美股暴跌的背后:特朗普错了还是“美联储疯了”?',
'宝马收购华晨宝马25%股权,股比增至75%,华晨或..',
'美国股市再度下挫 道指跌逾2%',
'美国三大股指再度集体下跌 道指跌逾2%']
In [3]: response.css(".ulist li a::attr(href)").extract()
Out[3]:
['http://business.sohu.com/20181011/n551939889.shtml',
'http://world.huanqiu.com/exclusive/2018-10/13238476.html',
'http://tech.hexun.com/2018-10-11/194724321.html',
'http://stock.hexun.com/2018-10-11/194710311.html',
'http://www.chinanews.com/gj/2018/10-12/8647813.shtml',
'http://auto.caijing.com.cn/2018/1011/4523312.shtml',
'http://stock.hexun.com/2018-10-12/194735122.html',
'http://www.jiemian.com/article/2531534.html',
'http://finance.ifeng.com/a/20181011/16524538_0.shtml',
'http://business.sohu.com/20181011/n551869431.shtml',
'http://finance.sina.com.cn/money/fund/fundzmt/2018-10-12/doc-ifxeuwws3364763.shtml',
'http://finance.qq.com/a/20181011/006267.htm',
'http://www.thepaper.cn/newsDetail_forward_2517733',
'http://money.163.com/18/1011/08/DTQTH7TT002580S6.html',
'http://money.163.com/18/1011/14/DTRI312V002581PP.html#f=post1603_tab_news',
'http://money.163.com/18/1012/07/DTTBDNTE00258152.html',
'http://finance.ifeng.com/a/20181011/16524323_0.shtml',
'http://news.hsw.cn/system/2018/1011/1030227.shtml',
'http://www.jiemian.com/article/2529148.html',
'http://stock.qq.com/a/20181011/010239.htm',
'http://www.cqcb.com/wealth/2018-10-11/1146722_pc.html',
'http://business.sohu.com/20181011/n551939889.shtml',
'http://world.huanqiu.com/exclusive/2018-10/13238476.html',
'http://tech.hexun.com/2018-10-11/194724321.html',
'http://stock.hexun.com/2018-10-11/194710311.html',
'http://www.chinanews.com/gj/2018/10-12/8647813.shtml',
'http://auto.caijing.com.cn/2018/1011/4523312.shtml',
'http://stock.hexun.com/2018-10-12/194735122.html',
'http://www.jiemian.com/article/2531534.html']
备注:特殊用法记录
li:nth-child(2):选择第2个li元素
li:nth-child(2n):选择第偶数个li元素
网友评论