scrapy-redis使用
技术栈:
python
scrapy
scrapy-redis
redis
mysql
windows
ubuntu
mysql:
1. 创建一个数据库tencent
2. 创建一个表tencent_table
create table tencent_table(
name varchar(100),
addr varchar(50),
time varchar(20),
type varchar(30),
responsibility varchar(1000),
requirement varchar(1000),
);
在ubuntu配置mysql的远程连接
同时配置redis的远程连接
招聘网站的抓取:
https://careers.tencent.com/search.html
1.页面分析
使用浏览器的开发者工具分析页面
查看网络–响应、预览
动态页面
2. check ajax异步请求
动态地址:
2.1 一级页面数据https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1622297360154&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=php&pageIndex=1&pageSize=10&language=zh-cn&area=cn
可以从json数据中,解析出详情页的PostId。
2.2 二级页面数据
https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1622303291537&postId=1398635661610721280&language=zh-cn
- 创建scrapy项目
在 items.py 定义抓取的字段
name/addr/time/type/responsibility/requirement - 编写爬虫文件,如下:
import scrapy
import time
import json
from ..items import TencentItem
class TencentSpider(scrapy.Spider):
name = 'tencent'
allowed_domains = ['careers.tencent.com']
#用户输入,如:python /java
input_key = input("输入岗位的开发语言类型, 如python:").strip()
#获取时间戳
ts = int(time.time()*1000)
first_url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp={}&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword={}&pageIndex=1&pageSize=10&language=zh-cn&area=cn"
start_urls = [first_url.format(ts,input_key)]
def parse