我想从cbfcindia中删除关于所有电影的数据。
1)在搜索框中,如果标题= "a“所有从"a”开始的电影都被填充,(在URL,va=a&Type=search中) http://cbfcindia.gov.in/html/uniquepage.aspx?va=a&Type=search
2)在一个表中填充了一个电影列表,现在这里是JAVASCRIPT,如果我单击第一个电影,我输入它的详细信息,我希望为所有的电影抓取所有这些细节。但我连一部电影都做不到。
3)我的观察:在来源中有以下功能:
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()
#passcbfcspider.py
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"发布于 2014-06-02 20:24:54
如果您深入了解源,每个链接都有以下标记:
<a id="DGMovie_ctl03_lnk" href="javascript:__doPostBack('DGMovie$ctl03$lnk','')">AGNI PARIKSHAYA</a>现在您知道了这个javascript函数是如何被实际调用的,您有了事件目标和事件参数的值。为了确保您在正确的轨道上,您还可以通过使用开发人员工具检查页面发生了什么,如果您正在使用chrome,请记住检查“保存日志”按钮。您将在href中看到第一个回发参数为EVENTTARGET。
使用正则表达式的xpath将为您提供所有回发参数:
sel.xpath("//*[contains(@id,'DGMovie')]/@href").re("doPostBack\(\'([^']+)")您需要向每个param发送邮件请求才能获得您的信息。请注意,您的网页使用iframes,所以您需要首先进入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)在爬行器中,您需要重构您的解析方法,以便它通过回调到FormRequest生成parse_items,而不是将解析逻辑移到parse_items (从解析)。
不要忘记分页,这也是通过回发完成的!
那些带有回发的asp.net页面通常最难解析。如果您感兴趣的话,请阅读更多关于它们的信息。
https://stackoverflow.com/questions/23991788
复制相似问题