您现在的位置是:课程教程文章

python scrapy.Request发送请求的方式

2023-12-14 20:14课程教程文章 人已围观

说明

1、使用scrapy.Request()指定method,body参数发送post请求。

2、使用scrapy.FormRequest()发送post请求,也可以发送表格和ajax请求。

实例

importscrapy


classGit2Spider(scrapy.Spider):
name='git2'
allowed_domains=['github.com']
start_urls=['http://github.com/login']

defparse(self,response):
username='GitLqr'
password='balabala'

#从登录页面响应中解析出post数据
token=response.xpath('//input[@name="authenticity_token"]/@value').extract_first()

post_data={
'commit':'Signin',
'authenticity_token':token,
'login':username,
'password':password,
'webauthn-support':'supported',
}
print(post_data)

#针对登录url发送post请求
yieldscrapy.FormRequest(
url='https://github.com/session',
callback=self.after_login,
formdata=post_data
)

defafter_login(self,response):
yieldscrapy.Request('https://github.com/GitLqr',callback=self.check_login)

defcheck_login(self,response):
print(response.xpath('/html/head/title/text()').extract_first())

以上就是python scrapy.Request发送请求的方式,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

课程教程:python scrapy.Request发送请求的方式

上一篇:python pipeline的使用注意

下一篇:没有了

站点信息

  • 文章统计篇文章