可以单独使用scrapy刮取iframe的内容吗?
作者:互联网
我尝试复制并粘贴网站的elements(xpath),但未返回任何结果.
可以抓取iframe内的数据吗?若是,应如何处理;若否,应做哪些其他事情?谢谢!
rules = (Rule (SgmlLinkExtractor(deny = path_deny_base, restrict_xpaths=('*'))
, callback="parse", follow= True),
)
def parse(self, response):
yield(Request(url, callback = self.parse_iframe))
def parse_iframe(self, response):
#your code to scrape the content from iframe
#def parse_items(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select('//div[2]/h1')
#//div[2]/h1
linker = hxs.select('//div[2]/div[10]/a[1]')
#//div[2]/div[10]/a[1]
loc_Con = hxs.select('//div[2]/div[1]/div[2]/span/span/span[1]') #//div[2]/div[1]/div[2]/span/span/span[1]
loc_Reg = hxs.select('//div[2]/div[1]/div[2]/span/span/span[2]') #/div[2]/div[1]/div[2]/span/span/span[2]
loc_Loc = hxs.select('//div[2]/div[1]/div[2]/span/span/span[3]') #/div[2]/div[1]/div[2]/span/span/span[3]
items = []
for titles in titles:
item = CraigslistSampleItem()
#item ["job_id"] = id.select('text()').extract()[0].strip()
item ["title"] = map(unicode.strip, titles.select('text()').extract()) #ok
item ["link"] = linker.select('@href').extract() #ok
item ["info"] = (response.url)
temp1 = loc_Con.select('text()').extract()
temp2 = loc_Reg.select('text()').extract()
temp3 = loc_Loc.select('text()').extract()
temp1 = temp1[0] if temp1 else ""
temp2 = temp2[0] if temp2 else ""
temp3 = temp3[0] if temp3 else ""
item["code"] = "{0}-{1}-{2}".format(temp1, temp2, temp3)
items.append(item)
return(items)
解决方法:
Scrapy无法从iframe抓取内容.而是您向iframe URL发出请求,例如:
def parse(self, response):
yield(Request(url, callback = self.parse_iframe))
def parse_iframe(self, response):
#your code to scrape the content from iframe
网址应为iframe网址,例如https://career-meridia……/jobs)
编辑:
将网址替换为带红色下划线的部分.
编辑2:
确保您已传递iframe网址所需的所有参数.否则,您将一无所获.如果是post方法,则必须传递所有post参数.
标签:web-scraping,scrapy,python 来源: https://codeday.me/bug/20191121/2053018.html