我很新的scrapy,也是我以前沒有在scrapy框架構建蟒在start_urls列表中的正則表達式URL
下使用正則表達式是我spider.py
代碼
class ExampleSpider(BaseSpider):
name = "test_code
allowed_domains = ["www.example.com"]
start_urls = [
"http://www.example.com/bookstore/new/1?filter=bookstore",
"http://www.example.com/bookstore/new/2?filter=bookstore",
"http://www.example.com/bookstore/new/3?filter=bookstore",
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
現在如果我們看看start_urls
所有這三個網址都是相同的,除非它們的整數值在2?, 3?
之間不同,我的意思是根據站點上存在的url進行無限制,現在我們可以使用爬網器,我們可以爲URL構建正則表達式,如下所示,
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
import re
class ExampleSpider(CrawlSpider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = [
"http://www.example.com/bookstore/new/1?filter=bookstore",
"http://www.example.com/bookstore/new/2?filter=bookstore",
"http://www.example.com/bookstore/new/3?filter=bookstore",
]
rules = (
Rule(SgmlLinkExtractor(allow=(........),))),
)
def parse(self, response):
hxs = HtmlXPathSelector(response)
可以請指導我,那我該如何構建一個爬行蜘蛛的規則爲上面的start_url
列表。
非常感謝你的非常有用的我,我得到了我的輸出 –
相信我會投票,怎樣才能使對x範圍無限的那個值,因爲實際上我每頁有20個項目(限於)(如果在頁面上添加了額外的項目,將會創建一個帶有遞增整數的URL,如上例所示),並且URL中存在的整數將增加。一種使這個範圍成爲無限的方法。 –
無限數意味着無限的處理時間。如果你仍然確定,做一個循環:'我= 0;而真:屈服...' – warvariuc