參考中文文檔
參考英文文檔
response.xpath
response.css 獲取相應(yīng)元素
namelist = response.xpath('//span[@class="text"]/text()').extract_first()
namelist = response.xpath('//span[@class="text"]/text()').extract
response.xpath('//a[contains(@href, "image")]/img/@src').extract()
if else 用法
title = articles.xpath("http://h1[@id='artical_topic']/text()").extract() if is_normal else \
response.xpath("http://div[@class='title']/h2/text()").extract()
a if a>b else b 上面代碼的\表示兩段代碼為一行
遇到403錯(cuò)誤無(wú)法爬取的情況 settings.py中添加
HTTPERROR_ALLOWED_CODES = [403]
BeautifulSoup用法
soup = BeautifulSoup(html, 'lxml')
soup.find //返回節(jié)點(diǎn)
soup.findAll('a',attrs={'id':'sdfsdf'}) //返回list list是各個(gè)節(jié)點(diǎn)
soup.findAll('a',attrs={'id':'sfsfss'})[0] //返回list的第一個(gè)元素
soup = soup.find('a')
soup['id'] = great //直接
xpath的使用 xpath不能跨標(biāo)簽 如果有多個(gè)標(biāo)簽需要查詢 使用|分割 意思是“或” extract()之后返回list
pic_urls = articles.xpath("http://div[@class='article-content']/img/@src|//div[@class='article-content']/div/p/img/@src").extract()