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)

这样就获取到了

相关推荐
万粉变现经纪人11 天前
如何解决 pip install bitsandbytes 报错 仅支持 Linux+glibc(macOS/Windows 失败)问题
linux·运维·windows·python·scrapy·macos·pip
yuanpan12 天前
Python Scrapy 入门教程:从零学会抓取和解析网页数据
java·python·scrapy
淮北49417 天前
ubuntu22.04将mp4转换成gif
python·scrapy·flask·beautifulsoup·pyqt·matplotlib
tIzE TERV19 天前
【爬虫】使用 Scrapy 框架爬取豆瓣电影 Top 250 数据的完整教程
爬虫·scrapy
NiKick19 天前
网页数据抓取:融合BeautifulSoup和Scrapy的高级爬虫技术
爬虫·scrapy·beautifulsoup
ZC跨境爬虫21 天前
Scrapy分布式爬虫(单机模拟多节点):豆瓣Top250项目设置与数据流全解析
分布式·爬虫·python·scrapy
ZC跨境爬虫22 天前
通俗易懂讲解分布式爬虫基础概念(附Scrapy-Redis实操教程)
redis·分布式·爬虫·python·scrapy
ZC跨境爬虫24 天前
Scrapy实战:5sing原创音乐网多页数据爬取(完整可运行,附避坑指南)
爬虫·python·scrapy·html
一点 内容24 天前
Scrapy框架深度解析:高效构建分布式爬虫的实战指南
分布式·爬虫·scrapy
ZC跨境爬虫1 个月前
Scrapy多级请求实战:5sing伴奏网爬取踩坑与优化全记录(JSON提取+Xpath解析)
爬虫·scrapy·html·json