class BaidufanyiSpider(scrapy.Spider):
name = 'baidufanyi'
allowed_domains = ['www.baidu.com']
# post请求时start_urls 没用,parse方法没用
# start_urls = ['http://www.baidu.com/']
# def parse(self, response):
# pass
def start_requests(self):
url = "https://fanyi.baidu.com/sug"
data = {
"kw": "love"
}
yield scrapy.FormRequest(url=url,
method="post",
formdata=data,
# 如果需要多次提交表单,且url一样,那么就必须加此参数dont_filter,防止被当成重复网页过滤掉了
dont_filter=False,
callback=self.parse_second,
meta={})
def parse_second(self, response):
content = response.text
obj = json.loads(content, encoding="utf8")
print(obj)