使用SCRAPY和PYTHON从Javascript中抓取数据
我想废弃有关cbfcindia所有电影的数据 1) 在搜索框中,如果Title=“a”填充了从“a”开始的所有电影,(在URL中,va=a&Type=SEARCH) 2) 电影列表填充在一个表中,现在这里是JAVASCRIPT,如果我点击第一部电影,我会输入它的详细信息,我希望为所有电影获取所有这些详细信息。 但即使是一部电影,我也无法做到这一点 3) 我的观察:在源代码中有以下功能:使用SCRAPY和PYTHON从Javascript中抓取数据,javascript,python,scrapy,Javascript,Python,Scrapy,我想废弃有关cbfcindia所有电影的数据 1) 在搜索框中,如果Title=“a”填充了从“a”开始的所有电影,(在URL中,va=a&Type=SEARCH) 2) 电影列表填充在一个表中,现在这里是JAVASCRIPT,如果我点击第一部电影,我会输入它的详细信息,我希望为所有电影获取所有这些详细信息。 但即使是一部电影,我也无法做到这一点 3) 我的观察:在源代码中有以下功能: function __doPostBack(eventTarget, eventArgument) {
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
我们需要基于JS传递参数。但我不知道该怎么做
items.py
from scrapy.item import Item, Field
class CbfcItem(Item):
MovieName = Field()
MovieLanguage = Field()
Roffice = Field()
CertificateNo = Field()
CertificateDate = Field()
Length = Field()
NameofProducer = Field()
#pass
from cbfc.items import CbfcItem
class MySpider(BaseSpider):
name = 'cbfc'
allowed_domains= ["http://cbfcindia.gov.in/"]
start_urls = ["http://cbfcindia.gov.in/html/uniquepage.aspx?va=a&Type=search"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//tbody") #Check
print titles
items = []
for titles in titles:
print "in FOR loop"
item = CbfcItem()
item ["MovieName"] = hxs.path('//*[@id="lblMovieName"]/text()').extract()
item ["MovieLanguage"] = hxs.path('//*[@id="lblLanguage"]').extract()
item ["Roffice"] = hxs.path('//*[@id="lblRegion"]').extract()
item ["CertificateNo"] = hxs.path('//*[@id="lblCertNo"]').extract()
item ["CertificateDate"] = hxs.path('//*[@id="Label1"]').extract()
item ["Length"] = hxs.path('//*[@id="lblCertificateLength"]').extract()
item ["NameofProducer"] = hxs.path('//*[@id="lblProducer"]').extract()
items.append(item)
print "this is ITEMS"
return items
print "End of FOR"
cbfcspider.py
from scrapy.item import Item, Field
class CbfcItem(Item):
MovieName = Field()
MovieLanguage = Field()
Roffice = Field()
CertificateNo = Field()
CertificateDate = Field()
Length = Field()
NameofProducer = Field()
#pass
from cbfc.items import CbfcItem
class MySpider(BaseSpider):
name = 'cbfc'
allowed_domains= ["http://cbfcindia.gov.in/"]
start_urls = ["http://cbfcindia.gov.in/html/uniquepage.aspx?va=a&Type=search"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//tbody") #Check
print titles
items = []
for titles in titles:
print "in FOR loop"
item = CbfcItem()
item ["MovieName"] = hxs.path('//*[@id="lblMovieName"]/text()').extract()
item ["MovieLanguage"] = hxs.path('//*[@id="lblLanguage"]').extract()
item ["Roffice"] = hxs.path('//*[@id="lblRegion"]').extract()
item ["CertificateNo"] = hxs.path('//*[@id="lblCertNo"]').extract()
item ["CertificateDate"] = hxs.path('//*[@id="Label1"]').extract()
item ["Length"] = hxs.path('//*[@id="lblCertificateLength"]').extract()
item ["NameofProducer"] = hxs.path('//*[@id="lblProducer"]').extract()
items.append(item)
print "this is ITEMS"
return items
print "End of FOR"
如果深入查看源代码,每个链接都有以下标记:
<a id="DGMovie_ctl03_lnk" href="javascript:__doPostBack('DGMovie$ctl03$lnk','')">AGNI PARIKSHAYA</a>
您需要对每个参数进行POST请求以获取您的信息。请注意,您的网页使用iframe,所以您需要首先进入iframe源
pawel@stack:~/stack$ scrapy shell "http://cbfcindia.gov.in/html/uniquepage.aspx?va=a&Type=search"
In [31]: url = sel.xpath("//iframe/@src").extract()[0]
In [33]: url
Out[33]: u'searchresults.aspx?va=a&Type=search'
In [35]: from urlparse import urljoin
In [36]: url = urljoin(response.url, url)
In [39]: from scrapy.http import Request
In [40]: req = Request(url)
in [41]: fetch(req)
# after fetching request..
In [48]: js_links = sel.xpath("//*[contains(@id,'DGMovie')]/@href").re("doPostBack\(\'([^']+)")
In [49]: param = js_links[0]
In [50]: param
Out[50]: u'DGMovie$ctl03$lnk'
In [51]: from scrapy.http import FormRequest
In [52]: fr = FormRequest.from_response(response, formdata={"__EVENTTARGET":param})
In [53]: fetch(fr)
2014-06-02 21:09:09+0100 [default] DEBUG: Redirecting (302) to <GET http://cbfcindia.gov.in/html/SearchDetails.aspx?mid=15&Loc=Backlog> from <POST http://cbfcindia.gov.in/html/searchresults.aspx?va=a&Type=search>
2014-06-02 21:09:10+0100 [default] DEBUG: Crawled (200) <GET http://cbfcindia.gov.in/html/SearchDetails.aspx?mid=15&Loc=Backlog> (referer: None)
In [54]: view(response)
pawel@stack:~/stack$scrapy shell“http://cbfcindia.gov.in/html/uniquepage.aspx?va=a&Type=search"
[31]中:url=sel.xpath(“//iframe/@src”).extract()[0]
在[33]中:url
Out[33]:u'searchresults.aspx?va=a&Type=search'
在[35]中:从urlparse导入urljoin
在[36]中:url=urljoin(response.url,url)
In[39]:来自scrapy.http导入请求
在[40]:req=请求(url)
in[41]:获取(请求)
#获取请求后。。
[48]:js_links=sel.xpath(“/*[contains(@id,'dgmoine')]/@href”).re(“doPostBack\(\'([^']+)”)
在[49]中:param=js_链接[0]
In[50]:param
输出[50]:u'DGMovie$ctl03$lnk'
在[51]中:从scrapy.http导入FormRequest
在[52]中:fr=FormRequest.from_response(response,formdata={“__EVENTTARGET”:param})
In[53]:获取(fr)
2014-06-02 21:09:09+0100[默认]调试:重定向(302)到
2014-06-02 21:09:10+0100[默认]调试:爬网(200)(参考:无)
在[54]:视图(响应)
在spider中,您需要重构您的解析方法,以便它生成带有回调的FormRequest来解析_项,而不是将解析逻辑移动到解析_项(从parse)
别忘了分页,回发也是如此
那些带有回发的asp.net页面通常是最难解析的。您在哪里找到的“”,只需在控制台中查找,使用开发人员工具悬停在每部电影上。我对cpan的内容一无所知。我面临类似的问题。请您指导。