scrapy post请求——百度翻译(十四)

scrapy处理 post 请求

爬取百度翻译界面

目录

1.创建项目及爬虫文件

2.发送post请求


1.创建项目及爬虫文件

scrapy startproject scrapy_104

scrapy genspider translate fanyi.baidu.com

2.发送请求

post请求需要传递参数,所以就不能用start_urls和parse函数了,这里使用start_requests函数给url添加参数。

python 复制代码
class TranslateSpider(scrapy.Spider):
    name = 'translate'
    allowed_domains = ['fanyi.baidu.com']
    # start_urls = ['http://fanyi.baidu.com/']

    def start_requests(self):
        url = 'http://fanyi.baidu.com/sug'

        data = {
            'kw':'spider'
        }

        yield scrapy.FormRequest(url=url, formdata=data,callback=self.parse_second)

    def parse_second(self, response):
        content = response.text
        # print(content)  # 存在编码问题
        obj = json.loads(content,encoding='utf-8')
        print(obj)

这样就获取到了

相关推荐
tang777892 小时前
Scrapy框架动态IP自动轮换集成配置教程
爬虫·tcp/ip·scrapy·架构·爬虫代理·动态ip
鬼手点金7 天前
Scrapy 网络爬虫框架
爬虫·python·scrapy·ajax·html·json·requsts
鬼手点金7 天前
Scrapy + Playwright 完整示例(JS 动态渲染网页)
开发语言·javascript·爬虫·python·scrapy·html·json
Data_Journal12 天前
如何使用 Java 和 Jsoup 解析 HTML
大数据·开发语言·数据库·python·scrapy
张小凡vip16 天前
python--爬虫--成熟的爬虫框架Scrapy
爬虫·python·scrapy
kisloy18 天前
【爬虫入门第13讲】Scrapy 框架深度解析(四)核心配置与自定义扩展
网络·爬虫·scrapy
kisloy22 天前
【爬虫入门第10讲】Scrapy 框架深度解析(一):五大组件与中间件
爬虫·scrapy·中间件
q5673152325 天前
Scrapy 框架集成稳定 HTTP 代理:中间件配置与断线重试实战
爬虫·网络协议·scrapy·http·中间件·http代理
小白学大数据1 个月前
两周完成爬虫技术栈升级:Scrapy 迁移 Crawlo 的路径与取舍
爬虫·python·scrapy
wangruofeng1 个月前
当 AI 打开浏览器:一次 LinkedIn 抓取背后的 8 层协议栈
scrapy·agent·ai编程