目标:为了获取东方财富网的上市公司减持信息,查看了下信息页面,属于单页面的信息,不需要使用scrapy,甚至于beautifulsoup也不用使用,只需要查看页面刷新数据时候访问的url即可。比起巨潮,东方财富网更简单的使用了get方法,并且参数使用都在页面加载返回的一个js里写的非常清楚,感觉就是为了方便爬取。。。同时,对参数pagenum都没有做限制,更加方便获取数据。唯一的难点在编解码,并不清楚为什么按照页面编码格式进行转换依旧会出现问题。
# -*- coding: GB2312 -*-
import requests
from bs4 import BeautifulSoup
import csv
import time
import codecs
#获取数据
def getHTML(url):
response = requests.get(url)
print(response.apparent_encoding) #返回页面编码:GB2312
#单用GB2312也可以好像
try:
print(response.text.encode('GB2312').decode('GB2312'))
except:
print(response.text.encode('utf-8').decode('utf-8'))
return response.text
#处理数据
def writeFile(writer,res):
#返回的数据:var TbrNdpvg={pages:401,data:[]},"url":""}
startid = res.find("[")
endid = res.find(