#### spider
```
import scrapy
from scrapy_tencent.items import ScrapyTencentItem
class TencentSpider(scrapy.Spider):
name = 'tencent'
allowed_domains = ['tencent.com']
start_urls = ['https://hr.tencent.com/position.php?&start=0#a']
def parse(self, response):
tr_list = response.xpath("//tr")[1:-2]
for tr in tr_list:
item = ScrapyTencentItem()
item["name"] = tr.xpath("./td[1]/a/text()").extract_first()
item["detail_url"] = "https://hr.tencent.com/"+tr.xpath("./td[1]/a/@href").extract_first()
item["category"] = tr.xpath("./td[2]/text()").extract_first()
item["city"] = tr.xpath("./td[4]/text()").extract_first()
#yield item
yield scrapy.Request(
item["detail_url"],
callback=self.parse_detail,
meta={"item1": item}
)
next_url = 'https://hr.tencent.com/' + response.xpath("//*[@id='next']/@href").extract_first()
if 'javascript:;' not in next_url:
yield scrapy.Request(url=next_url, callback=self.parse)
def parse_detail(self, response):
item = response.meta['item1']
item['duty'] = response.xpath('//tr[3]/td/ul/li/text()').extract()[0]
item['require'] = response.xpath('//tr[4]/td/ul/li/text()').extract()[0]
# 返回item給引擎
yield item
```
#### items
```
import scrapy
class ScrapyTencentItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
category = scrapy.Field()
city = scrapy.Field()
detail_url = scrapy.Field()
duty = scrapy.Field()
require = scrapy.Field()
```
####
```
import json
from scrapy_tencent.items import ScrapyTencentItem
class ScrapyTencentPipeline(object):
def open_spider(self, item):
self.file = open("tencent.json", "w")
def process_item(self, item, spider):
# 保存json到文件
if isinstance(item, ScrapyTencentItem):
item_json = json.dumps(dict(item), ensure_ascii=False) + ',\n'
self.file.write(item_json)
return item
def close_spider(self):
self.file.close()
```
- 基礎
- 輸入輸出
- 常量變量
- 常量
- 變量
- 數據類型
- 簡單數據類型
- 整型
- 浮點型
- 復數
- 復雜數據類型
- 序列
- 通用操作
- 字符串
- 定義
- 方法
- 格式化
- eval
- range
- capitalize
- count
- 格式化
- list
- 定義
- 方法
- tuple
- 定義
- 特點
- 方法
- dict
- 定義
- set
- 定義
- 類型轉換
- 函數
- 定義
- 參數
- 函數變量
- 多返回值
- 作用域
- 函數嵌套
- 匿名函數
- 面向對象
- 類
- 對象
- 封裝
- 私有
- property
- 類屬性和實例屬性
- 實例方法、靜態方法和類方法
- 繼承
- 多態
- 接口
- 設計原則
- 開放/封閉原則
- 控制反轉原則
- 接口隔離原則
- 單一職責原則
- 替換原則
- 魔法方法
- 構造方法
- 析構方法
- 靜態方法
- doc
- call
- dict
- str
- 異常
- with
- 提高
- 深淺拷貝
- 閉包
- 裝飾器
- 正則表達式
- 單個字符
- 多個字符
- 開頭結尾
- 分組
- 高級語法
- 網絡編程
- 多任務
- 線程
- 創建線程
- 主線程等待
- 共享全局變量
- 傳遞參數
- 進程
- 創建
- 進程間通信
- 協程
- 數據庫
- SQL
- 安裝
- 備份
- 查詢
- 消除重復行
- 分組
- 字查詢
- Python 中操作 MySQL
- 設計模式
- 簡介
- 三大類
- 創建型
- 單例
- 常用模塊
- 導入模塊
- 系統內置
- os
- time
- math
- help
- logging
- 格式
- 輸出到控制臺
- 輸出到文件
- 兩個都輸出
- 三方模塊
- Tesseract
- redis
- 安裝
- selenium
- Selenium
- PhantomJS
- Chromedriver
- turtle
- 五角星
- 正方體
- urllib3
- http請求
- Requests
- 基本使用
- 發送請求
- cookies
- 代理
- Django
- 框架介紹
- 項目搭建
- 項目配置
- 路由
- 項目urls.py
- 應用urls.py
- 控制器
- request
- response
- Cookie
- Session
- views
- Models
- 數據庫
- ORM
- 配置和遷移
- 模型類
- 模型管理器
- 增刪改
- 查
- 查詢集
- Mysql日志
- 自定義模型管理器
- 中間件
- scrapy
- 工作原理
- 創建項目
- spider
- items
- pipeline
- settings
- logging
- 小例子
- 果殼網
- 人人網登陸
- 騰訊招聘
- 創建模塊
- 網絡爬蟲
- 基礎知識
- 爬蟲的用途
- robots協議
- http和https
- 常見請求頭
- 狀態碼
- 字符串
- requests
- 簡介
- 發送請求
- response
- 小技巧
- 代理
- cookie和session
- Fiddler
- 數據提取
- 數據分類
- json
- 正則
- xpath
- lxml
- 自動化運維
- Shell
- 開發規范
- 注釋
- 腳本執行
- 變量
- 表達式
- 測試語句
- 條件表達式
- 邏輯表達式
- 文件表達式
- 數值操作符
- 字符串比較
- 計算表達式
- 常見符號
- 重定向符號
- 管道符
- 其他符號
- 常見命令
- grep
- sed
- awk
- find
- 流程控制
- if