过去 3 年我一直在用 PHP 编写数据抓取脚本。
这是一个简单的PHP脚本
$url = 'https://appext20.dos.ny.gov/corp_public/CORPSEARCH.SELECT_ENTITY';
$fields = array(
'p_entity_name' => urlencode('AAA'),
'p_name_type' => urlencode('A'),
'p_search_type' => urlencode('BEGINS')
);
//url-ify the data for the POST
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
print curl_error($ch) . '<br>';
print curl_getinfo($ch, CURLINFO_HTTP_CODE) . '<br>';
print $result;
仅当 CURLOPT_SSL_VERIFYPEER 为 false 时它才能正常工作。如果我们启用 CURLOPT_SSL_VERIFYPEER 或使用 http 而不是 https,它会返回空响应。
但是,我必须在 Python Scrapy 中做同样的项目,这里是 Scrapy 中的相同代码。
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from scrapy.http.request import Request
import urllib
from appext20.items import Appext20Item
class Appext20Spider(CrawlSpider):
name = "appext20"
allowed_domains = ["appext20.dos.ny.gov"]
DOWNLOAD_HANDLERS = {
'https': 'my.custom.downloader.handler.https.HttpsDownloaderIgnoreCNError',}
def start_requests(self):
payload = {"p_entity_name": 'AMEB', "p_name_type": 'A', 'p_search_type':'BEGINS'}
url = 'https://appext20.dos.ny.gov/corp_public/CORPSEARCH.SELECT_ENTITY'
yield Request(url, self.parse_data, method="POST", body=urllib.urlencode(payload))
def parse_data(self, response):
print('here is repos')
print response
它返回空响应。需要禁用 SSL 验证。
请原谅我对 Python Scrapy 缺乏了解,我已经搜索了很多,但没有找到任何解决方案。
请您参考如下方法:
我建议您查看此页面:http://doc.scrapy.org/en/1.0/topics/settings.html看起来您可以改变模块的行为方式并更改各种处理程序的设置。
我也相信这是一个重复的问题:Disable SSL certificate verification in Scrapy
HTH
谢谢,
//P
