1.參考
pyspider作者官網:
pyspider 爬蟲教程(一):HTML 和 CSS 選擇器
pyspider 爬蟲教程(三):使用 PhantomJS 渲染帶 JS 的頁面
其他:
2.jQuery 語法
jQuery 是一個 JavaScript 庫。jQuery 極大地簡化了 JavaScript 編程。
http://www.w3school.com.cn/jquery/jquery_syntax.asp
http://www.w3school.com.cn/jquery/jquery_selectors.asp
http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp
語法 描述
$(this) 當前 HTML 元素
$("p") 所有 <p> 元素
$("p.intro") 所有 class="intro" 的 <p> 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的元素
$("ul li:first") 每個 <ul> 的第一個 <li> 元素
$("[href]") 選取所有帶有 href 屬性的元素。
$("[href='#']") 選取所有帶有 href 值等於 "#" 的元素。
$("div#intro .head") id="intro" 的 <div> 元素中的所有 class="head" 的元素
jQuery CSS 選擇器可用於改變 HTML 元素的 CSS 屬性。!!!!!
$("p").css("background-color","red");
3.pyspider相關
(1) 全局配置,調試時每次修改代碼記得更新itag,save,刷新頁面再run
class Handler(BaseHandler): crawl_config = { 'itag': 'v001', 'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'} }
(2) response.save用於傳遞數據
file_name = 'xxx' self.crawl(img.attr.src, callback=self.save_img, save={'file_name': file_name}) def save_img(self,response): file_name = response.save['file_name'] #使用傳入的數據
(3) 如果返回結果為多項的json,無法每一項返回一個條目。。。
# https://binux.blog/2015/01/pyspider-tutorial-level-2-ajax-and-more-http/
# 我測試返回的數據不是預期(我想每個電影為顯示一行)
# 你想要在界面中顯示為多行?這是不行的,results 頁面永遠只會每個 url 返回一個結果。如果這個結果是一個數組,那么就顯示一個數組。
def json_parser(self, response): return [{ "title": x['title'], "rate": x['rate'], "url": x['url'] } for x in response.json['subjects']]
(4) 操作pyspider的response.doc
xml_doc = ''' <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>''' from pyquery import PyQuery response_doc = PyQuery(xml_doc) #response_doc即pyspider的response.doc for each in response_doc('book>title').items(): #括號內部使用CSS選擇tag,.items()用來遍歷 print each.attr.lang print each.text() print 123
(5) 操作pyspider的response.etree
html_doc = ''' <div> <ul> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div>''' import lxml.html response_etree = lxml.html.fromstring(html_doc) #response_etree即pyspider的response.etree for each in response_etree.xpath('//ul/li/a'): print each.xpath('./@href') #返回列表 [0]可能報錯 print each.text print 123 print for each in response_etree.cssselect('div li a'): print each.get('href') #推薦或返回None print each.text print 123 print
(6) 方便本地調試response.doc 和 response.etree
url = 'http://movie.douban.com/top250' headers={'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'} # response_doc = PyQuery(url,headers=headers) request = urllib2.Request(url,headers=headers) f = urllib2.urlopen(request) # Note: this returns a tree, not an element. Use``parse(...).getroot()`` to get the document root. response_etree = lxml.html.parse(f).getroot() #.getroot() for each in response_etree.xpath('//a[contains(@href,"subject")]'): print each.xpath('./@href')[0] print for each in response_etree.cssselect('a[href*="subject"]'): print each.get('href') print # pyspider源代碼為doc=PyQuery(etree) response_doc = PyQuery(response_etree) for each in response_doc('a[href*="subject"]').items(): print each.attr.href