微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python scrapy 表单post rest api接口返回json写wordpress

import scrapy
import json
from urllib.parse import urlencode
from scrapy.crawler import CrawlerProcess
from scrapy import Spider, FormRequest
from wordpress_xmlrpc import Client, wordpressPost
from wordpress_xmlrpc.methods.posts import NewPost

id = 'username'
password	= 'pwd'
publish_url = 'http://wordpress.url/xmlrpc.PHP'
source_url	= 'http://gubatopic.eastmoney.com/interface/GetData.aspx'
post_data	={"param": "ps%3D7%26p%3D1%26type%3D0","path":"newtopic%2Fapi%2FTopic%2FHomePagelistRead","env":"2"}

class EmSpider(Spider):
    name = "em"
    def start_requests(self):
        yield FormRequest(source_url, formdata=post_data)

    def parse(self,response):
        for item in json.loads(response.text)['re']:
            print(item['nickname']+' ,'+str(item['htid']))
            wp = Client(publish_url, id, password)
            post = wordpressPost()
            post.post_status = 'publish'
            post.title = item['nickname']
            post.content = str(item['htid'])
            #post.excerpt = '这里是摘抄样式示例'
            post.terms_names = {
            	"post_tag": ['Python'],
            	"category": ['Python']
            }
            wp.call(NewPost(post))				

  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐