官术网_书友最值得收藏!

How to do it...

We start by importing Selector from scrapy, and also requests so that we can retrieve the page:

In [1]: from scrapy.selector import Selector
...: import requests
...:

Next we load the page.  For this example we are going to retrieve the most recent questions on StackOverflow and extract their titles.  We can make this query with the the following:

In [2]: response = requests.get("http://stackoverflow.com/questions")

Now create a Selector and pass it the response object:

In [3]: selector = Selector(response)
...: selector
...:
Out[3]: <Selector xpath=None data='<html>\r\n\r\n <head>\r\n\r\n <title>N'>

Examining the content of this page we can see that questions have the following structure to their HTML:

The HTML of a StackOverflow Question

With the selector we can find these using XPath:

In [4]: summaries = selector.xpath('//div[@class="summary"]/h3')
...: summaries[0:5]
...:
Out[4]:
[<Selector xpath='//div[@class="summary"]/h3' data='<h3><a href="/questions/48353091/how-to-'>,
<Selector xpath='//div[@class="summary"]/h3' data='<h3><a href="/questions/48353090/move-fi'>,
<Selector xpath='//div[@class="summary"]/h3' data='<h3><a href="/questions/48353089/java-la'>,
<Selector xpath='//div[@class="summary"]/h3' data='<h3><a href="/questions/48353086/how-do-'>,
<Selector xpath='//div[@class="summary"]/h3' data='<h3><a href="/questions/48353085/running'>]

And now we drill a little further into each to get the title of the question.

In [5]: [x.extract() for x in summaries.xpath('a[@class="question-hyperlink"]/text()')][:10]
Out[5]:
['How to convert stdout binary file to a data URL?',
'Move first letter from sentence to the end',
'Java launch program and interact with it programmatically',
'How do I build vala from scratch',
'Running Sql Script',
'Mysql - Auto create, update, delete table 2 from table 1',
'how to map meeting data corresponding calendar time in java',
'Range of L*a* b* in Matlab',
'set maximum and minimum number input box in js,html',
'I created generic array and tried to store the value but it is showing ArrayStoreException']
主站蜘蛛池模板: 姚安县| 大城县| 双城市| 高阳县| 东乡族自治县| 牡丹江市| 普兰县| 牡丹江市| 武定县| 高青县| 阳西县| 壤塘县| 思茅市| 洛川县| 六盘水市| 福安市| 谷城县| 五台县| 塘沽区| 潜江市| 保德县| 阿拉善左旗| 报价| 嵊州市| 策勒县| 海晏县| 宜宾县| 济南市| 江陵县| 邛崃市| 唐海县| 马龙县| 兰坪| 连云港市| 原阳县| 资源县| 大港区| 韶关市| 长岭县| 兴隆县| 工布江达县|